我正在尝试创建一个下拉指令,我想指定选择ng-options“选项值”和“选项描述”属性,方法是使用指令属性指定它们.请参阅代码以便更好地理解……
这是我的指示.这显然不起作用,但我认为它将描述我正在尝试做什么……
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; } };
});
……这就是我想用它的方式
<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>
小提琴:http://jsfiddle.net/wXV6Z/1/
如果你有解决这个问题的方法,或者更有可能,对如何处理它有不同的意见,请告诉我!
谢谢
/安德烈亚斯