我試圖預先選擇多個對象到jQuery中使用Angular選擇控件。見我Plunkr這裏:http://plnkr.co/FNlTAcILnoeyjZat1tH1更新jQuery選擇Angular的值
的數據被設置到模型在我的控制器像這樣:
$scope.valueTypes = [
{ code: 'V1', name: 'Value 1', description: 'Description 1' },
{ code: 'V3', name: 'Value 3', description: 'Description 3' }
];
我選擇的包裝指令看起來像這樣:
.directive('myChosen', ['$log', function ($log) {
return {
link: function (scope, element, attrs) {
var model = attrs.ngModel;
if (model !== null) {
scope.$watch(model, function (data) {
$log.info('Model updated: ' + angular.toJson(data));
element.trigger('chosen:updated');
});
}
element.chosen();
},
require: 'ngModel',
restrict: 'A'
};
}])
和使用方法如下:
<select id="valueType" name="valueType" class="form-control chosen-select" ng-options="valueType as valueType.code + ' - ' + valueType.name for valueType in getValueTypes()" ng-model="valueTypes" multiple="" my-chosen ng-required="true">
with getValueTypes()返回對象列表:
$scope.getValueTypes = function() {
var valueTypes = [];
for (var i = 0; i < 5; i ++) {
valueTypes.push({
code: 'V' + i,
name: 'Value ' + i,
description: 'Description ' + i
});
}
return valueTypes;
};
但是,沒有預先選定。如果我切換到我的選擇列表和預選值的字符串列表,則會正確預選值。
那麼,我該如何做預選工作呢?
謝謝!
這是你的小提琴,不是你的朋友.. –
@IsaacKleinman righty right; o – nilsK