我有以下穿梭箱指令:
<shuttle-Boxes ng-options="item for item in itemList" ng-model="myModel" />
我的模板看起来像这样:
template: '<div class="shuttle-Boxes">' + '<select multiple="multiple" class="shuttle-Boxes-left"></select>' + '<button class="shuttle-Boxes-btn" type="button"><i class="icon icon-arrow-right"></i></button>' + '<button class="shuttle-Boxes-btn" type="button"><i class="icon icon-arrow-left"></i></button>' + '<select multiple="multiple" class="shuttle-Boxes-right"></select>' + '</div>'
我想要做的是从< shuttle-Boxes>中取出ng-options转发器.并用它来填充< select class =“shuttle-Boxes-left”>.
我试图这样做的方法是无效的只是将ng-options属性复制到选择列表,如下所示:
var availableList = cElement.find('.shuttle-Boxes-left'),repeater = cAttrs.ngOptions; availableList.attr('ng-options',repeater);
这是小提琴:http://jsfiddle.net/dkrotts/tHTAY/1/
我究竟做错了什么?
我建议你只将itemList传递给指令(和myModel)并将ng-options放在你的模板中:
<shuttle-Boxes items="itemList" ng-model="myModel"></shuttle-Boxes>
指示:
myApp.directive('shuttleBoxes',function($timeout) { return { restrict: 'E',scope: { items: '=',ngModel: '='},template: '<div class="shuttle-Boxes">' + '<select multiple="multiple" class="shuttle-Boxes-left" ng-options="item for item in items" ng-model="ngModel"></select>' + '<button class="shuttle-Boxes-btn" type="button">' + '<i class="icon icon-arrow-right"></i></button>' + '<button class="shuttle-Boxes-btn" type="button">' + '<i class="icon icon-arrow-left"></i></button>' + '<select multiple="multiple" class="shuttle-Boxes-right"></select>' + '</div>',replace: true } });