6

我有以下穿梭箱指令:

<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>'

我想要做的是从 ng-options 中继器中获取<shuttle-boxes>并使用它来填充<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/

我究竟做错了什么?

4

1 回答 1

8

我建议您只将 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
    }
});

小提琴

于 2013-01-29T15:50:44.717 回答