范围的分配不适用于指令

时间:2016-07-11 11:06:05

标签: angularjs angularjs-directive

我在下面有一个指令,它在范围元素sections.data中进行了更改,后来我assign更新了UI上的数据。但是,此方法适用于函数change_subnav,但不适用于switch案例(将注释添加到代码中)。为什么会这样?此更改的代码是相同的。将在这里感谢任何帮助。如果我需要添加更多信息,请告诉我。

Plunker - https://embed.plnkr.co/wmoJcQ/

.directive('selectSubnav', function ($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {

            var change_subnav = function (subnav) {
                if (scope.active_tab == 'user_exercises') {
                    var sections = {};
                    sections[subnav] = scope[scope.active_tab][subnav];
                    // this works
                    scope.$apply(function () {
                        $parse('sections.data').assign(scope.$parent, sections);
                    });
                } else {

                }
            };

            $(element).on('click', function () {
                $(element).parent().children().removeClass('active');
                $(element).addClass('active');

                switch (attr.selectSubnav) {
                    case 'All':
                        // this doesn't work
                        scope.$apply(function () {
                            $parse('sections.data').assign(scope.$parent, scope[scope.active_tab]);
                            console.log(scope.sections.data);
                        });

                        break;
                    default:
                        change_subnav(attr.selectSubnav);
                        break;
                }
            });
        }
    }
})

更新

.directive('selectSubnav', function ($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
            var currentScope = scope;
            var change_subnav = function (subnav) {
                if (scope.active_tab == 'user_exercises') {
                    var sections = {};
                    sections[subnav] = scope[scope.active_tab][subnav];
                } else {

                }

                return sections;
            };

            $(element).on('click', function () {
                $(element).parent().children().removeClass('active');
                $(element).addClass('active');

                var sections;

                switch (attr.selectSubnav) {
                    case 'All':
                        // this doesn't work
                        sections = currentScope[currentScope.active_tab];
                        break;
                    default:
                        sections = change_subnav(attr.selectSubnav);
                        break;
                }

                currentScope.$apply(function () {
                    $parse('sections.data').assign(currentScope.$parent, sections);
                });
            });
        }
    }
})

1 个答案:

答案 0 :(得分:0)

您已经可以访问链接功能中的范围,因此不要像这样更新scope.sections.data:

$parse('sections.data').assign(scope.$parent, sections);

你可以这样做:

$parse('sections.data').assign(scope, sections);

或者,更好的是直接更新它,如下所示:

scope.sections.data = sections;