javascript - Chosen Angular directive doesn't get updated -
i've followed great tutorial (link) chosen , angular (code pretty same)
here directive:
app.angularmodule.directive('chosen', function() { var linker = function (scope, element, attrs) { var list = attrs['chosen']; scope.$watch(list, function () { element.trigger('chosen:updated'); }); element.chosen({ width: '350px'}); }; return { restrict: 'a', link: linker }; });
here html:
<select data-placeholder="choose category" multiple class="col-lg-8 chosen-select" chosen="items" ng-options="item._backingstore.name item in items" ng-model="selectedcategories" > </select>
what want is, when user clicks edit button, modal window pops up, , categories selected before clicking edit button, selected in modal window.
here part of controller:
$scope.$watch(function() { return admincrudservice.getcategoriesforupdate(); }, function() { $scope.action = "edit"; $scope.categoriesforupdate = admincrudservice.getcategoriesforupdate(); if ($scope.categoriesforupdate.length > null) { $scope.selectedcategories = _.filter($scope.items, function (item) { return _.contains($scope.categoriesforupdate, item); }); } });
i've logged $scope.selectedcategories , fine them, reason there nothing selected in chosen.
so doing wrong , how can fix ?
edit
i've noticed when select items, close modal, open again, selected values there again eve though put line inside $watch
$scope.selectedcategories = "";
edit 2
so left problem while, because had more important things deal with. i've tried without chosen, i.e. using "normal" select , code works. definitively chosen directive doesn't work should.
i've solved it, solution pretty easy , straightforward (when how angular directives work). here whole code directive:
app.angularmodule.directive('chosen', function() { var linker = function (scope, element, attrs) { var list = attrs['chosen']; scope.$watch(list, function () { element.trigger('chosen:updated'); }); scope.$watch(attrs['ngmodel'], function() { element.trigger('chosen:updated'); }); element.chosen({ width: '350px'}); }; return { restrict: 'a', link: linker }; });
Comments
Post a Comment