gpt4 book ai didi

angularjs - 在指令中使用 ngOptions

转载 作者:行者123 更新时间:2023-12-03 08:16:49 25 4
gpt4 key购买 nike

我有以下穿梭箱指令:

<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);

这是 fiddle : 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
}
});

Fiddle .

关于angularjs - 在指令中使用 ngOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14586249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com