I followed this wonderful tutorial ( link ) for Chosen and Angular (the code is pretty much the same)
Here is my 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 is the html:
<select data-placeholder="Choose a Category" multiple class="col-lg-8 chosen-select" chosen="items" ng-options="item._backingStore.Name for item in items" ng-model="selectedCategories" > </select>
What I want, when the user clicks the edit button, a modal window appears, and the categories that are selected before clicking the edit button are selected in the modal window.
Here is this part of the 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 registered $ scope.selectedCategories, and everything is fine with them, but for some reason nothing was selected in the selected one.
So what am I doing wrong and how can I fix this?
EDIT
I noticed when I select some elements, close the modal, open them again, the selected values ββappear again, although I put this line inside $ watch
$scope.selectedCategories = "";
EDIT 2
So, I left this problem for a while, because I had a more important case. I tried without a choice, i.e. Using the "normal" selection and operation of the code. So, finally, my chosen directive does not work as it should.