AngularJS:带有自定义ng选项的下拉指令
我正在尝试创建一个下拉指令,其中我希望通过使用指令属性指定选项“option value”和“option description”属性来指定它们。请参阅代码以更好地理解 这是我的指示。这显然是行不通的,但我想它会描述我正在努力做的事情AngularJS:带有自定义ng选项的下拉指令,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我正在尝试创建一个下拉指令,其中我希望通过使用指令属性指定选项“option value”和“option description”属性来指定它们。请参阅代码以更好地理解 这是我的指示。这显然是行不通的,但我想它会描述我正在努力做的事情 app.directive('dropdown', function(){ return { restrict: 'E', scope: { array: '=' }, template: '<label
app.directive('dropdown', function(){
return {
restrict: 'E',
scope: {
array: '='
},
template: '<label>{{label}}</label>' +
'<select ng-model="ngModel" ng-options="a[{{optValue}}] as a[{{optDescription}}] for a in array">' +
'<option style="display: none" value="">-- {{title}} --</option>' +
'</select>',
link: function (scope, element, attrs) {
scope.label = attrs.label;
scope.title = attrs.title;
scope.optValue = attrs.optValue;
scope.optDescription = attrs.Description;
}
};
app.directive('dropdown',function(){
返回{
限制:'E',
范围:{
数组:'='
},
模板:“{label}}”+
'' +
“{{title}}--”+
'',
链接:函数(范围、元素、属性){
scope.label=attrs.label;
scope.title=attrs.title;
scope.optValue=attrs.optValue;
scope.optDescription=attrs.Description;
}
};
}))
…下面是我想如何使用它
<dropdown title="Choose ferry" label="Ferries" array="ferries" opt-value="Id" opt-description="Description"></dropdown>
<dropdown title="Choose route" label="Routes" array="routes" opt-value="Code" opt-description="Name"></dropdown>
和小提琴:
如果你对这个问题有一个解决方案,或者更可能,对如何解决它有不同的意见,请让我知道
谢谢
/安德烈亚斯事实上,这是可行的。您需要做的唯一更改是删除
a[{{optValue}}]
和a[{{optDescription}}]
周围的大括号,因为您不需要在那里插入
optValue
和optDescription
已经是作用域中的字符串,因此a[optValue]
和a[optDescription]
将使代码正常工作,因为它们是在指令作用域中计算的表达式
.这是个好电话。由于每个部分都是通过Angular进行评估的,所以它将能够将这些值从$scope中拉出。这正是我要寻找的。非常感谢你!这帮助我解决了我在自己的定制指令中遇到的一个问题,做了一些非常类似的事情。谢谢