选择的角度指令没有更新 [英] Chosen Angular directive doesn't get updated

查看:118
本文介绍了选择的角度指令没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照拣选和角度(这个伟大的教程(链接) code是pretty很多相同)

I've followed this great tutorial (link) for Chosen and Angular (code is pretty much same)

下面是我的指令:

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
    };
});

下面是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 is, when user clicks edit button, modal window pops up, and categories that where selected before clicking edit button, are selected in modal window.

下面是控制器的一部分:

Here is that 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);
                    });
                }
            });

我已经登录的 $ scope.selectedCategories 的,一切都很好和他们在一起,但由于某种原因没有什么选择。选择

I've logged $scope.selectedCategories and everything is fine with them, but for some reason there is nothing selected in chosen.

那么,我做错了,我该如何解决这个问题?

So what am I doing wrong and how can I fix it ?

修改

当我选择了一些项目,关闭模式,再次打开它,我已经注意到,选定值有再次前夕,虽然我把里面的$这一行看

I've noticed when I select some items, close modal, open it again, selected values are there again eve though i put this line inside $watch

$scope.selectedCategories = "";

编辑2

所以,我离开这个问题了一会儿,因为我不得不处理更重要的事情。
我试过没有选择,即用正常的选择和code ++工程。所以我明确选择指令不能正常工作,因为它应该。

So I left this problem for a while, because I had more important things to deal with. I've tried without chosen, i.e. using "normal" select and code works. So Definitively my chosen directive doesn't work as it should.

推荐答案

我已经解决了,解决的办法是pretty简单,实际上简单(当你角指令的工作原理)。这里是整个code的指令:

I've solved it, the solution is pretty easy and straightforward actually (when you get how Angular directives work). Here is whole code for 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
    };
});

这篇关于选择的角度指令没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆