admin管理员组文章数量:1391981
Official example for Angular-ui select2 tags is:
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.
};
});
And I have this piece of code:
$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);
});
}
});
But this just doesn't work, when I click on Selcet2, I get No matches found and I've logged $scope.categoryNames inside $watch, and data is there (so that part works fine).
So my question is can tags be loaded dynamically, and if they can, how ?
Official example for Angular-ui select2 tags is:
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.
};
});
And I have this piece of code:
$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);
});
}
});
But this just doesn't work, when I click on Selcet2, I get No matches found and I've logged $scope.categoryNames inside $watch, and data is there (so that part works fine).
So my question is can tags be loaded dynamically, and if they can, how ?
Share Improve this question asked Aug 14, 2013 at 23:28 hyperNhyperN 2,75410 gold badges58 silver badges96 bronze badges2 Answers
Reset to default 9I've recently encountered this issue when using the AngularUI Select2 project, and solved it by making the tags argument a function that returns the model. For example:
$scope.select2Options = {
multiple: true,
simple_tags: true,
tags: function () {
return $scope.categoryNames;
}
};
Any updates to the $scope.categoryNames
is reflected in the view because Select2 calls the tags
function when opened and on every key press.
Hopefully this is useful for people wanting to use Select2 rather than Chosen.
Well, I couldn't get it to work, so I've deiced to use Chosen instead, following this great tutorial: link and I got result I wanted in no-time.
本文标签: javascriptAngularUI select2 dynamically change tags attribute in optionsStack Overflow
版权声明:本文标题:javascript - Angular-UI select2 dynamically change tags attribute in options - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744655105a2617918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论