[英]Angular-UI select2 dynamically change tags attribute in options
Angular-ui select2标签的官方示例是:
myAppModule.controller('MyController', function($scope) {
$scope.list_of_string = ['tag1', 'tag2']
$scope.select2Options = {
'multiple': true,
'simple_tags': true,
'tags': ['tag1', 'tag2', 'tag3', 'tag4'] // Can be empty list.
};
});
我有这段代码:
$scope.select2Options = {
'multiple': true,
'simple_tags': true,
'tags': $scope.categoryNames
};
$scope.$watch(function () { return adminCrudService.getCategoriesForUpdate(); }, function () {
$scope.action = "edit";
$scope.categoriesForUpdate = adminCrudService.getCategoriesForUpdate();
if ($scope.categoriesForUpdate.length > null) {
$scope.categoryNames = [];
_.forEach($scope.categoriesForUpdate, function (item) {
$scope.categoryNames.push(item._backingStore.Name);
});
}
});
但这是行不通的,当我单击Selcet2时, 没有找到匹配项,并且在$ watch中记录了$ scope.categoryNames ,并且有数据(因此该部分工作正常)。
所以我的问题是标签可以动态加载吗?如果可以,如何加载?
我最近在使用AngularUI Select2项目时遇到了此问题,并通过使tags参数成为返回模型的函数来解决了该问题。 例如:
$scope.select2Options = {
multiple: true,
simple_tags: true,
tags: function () {
return $scope.categoryNames;
}
};
$scope.categoryNames
任何更新$scope.categoryNames
反映在视图中,因为Select2在打开时以及每次按键时都会调用tags
功能。
希望这对希望使用Select2而不是选择的用户有用。
好吧,我无法使它正常工作,所以我遵循这个很棒的教程: link ,选择使用Chosen,我很快就得到了想要的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.