javascript - 当父范围变量更改时调用指令中的函数

标签 javascript angularjs

当父 Controller 中的变量值发生变化时,我需要在指令中调用函数。我尝试添加一个 watch (我显然做错了),因为当值发生变化时没有任何反应。这是指令:

    angular.module('ssq.shared').directive('checkboxPicklist', function() {
    return {
        restrict: 'E',
        templateUrl: '/Scripts/app/Shared/directives/checkboxPicklist.html',
        replace: true,
        scope: {
            itemId: '=',
            list: '=',
            nameProp: '=',
            title: '@',
            searchPlaceholder: '@',
            callbackFn: '&',
            callMore: '&',
            clear: '='
        },

        link: function (scope, element, attrs) {
            scope.query = '';
            var parent = scope.$parent;
            var clear = parent.clear;
            scope.$watch(clear, function () {
                if (clear == true) {
                    this.clearAll();
                }
            })
            var child = element.find('.dropdown-menu');
            child.on({
                'click': function (e) {
                    e.stopPropagation();
                }
            });


            var selectedItemFn = function (item) {
                return item.selected;
            };

            scope.getSelectedCount = function () {
                return _.filter(scope.list, selectedItemFn).length;
            };
            scope.loadMore = function () {
                scope.callMore();
            };

            scope.allSelected = function(list) {
                var newValue = !scope.allNeedsMet(list);
                _.each(list, function(item) {
                    item.selected = newValue;
                    scope.callbackFn({ object: item });
                });
            };

            scope.allNeedsMet = function(list) {

                var needsMet = _.reduce(list, function(memo, item) {
                    return memo + (item.selected ? 1 : 0);
                }, 0);
                if (!list) {
                    return (needsMet === 0);
                }
                return (needsMet === list.length);
            };

            function clearAll() {
                _.each(list, function (item) {
                    item.selected = false;
                })
            }
        }

    };
});

这是我试图观察变量的地方:

            var parent = scope.$parent;
            var clear = parent.clear;
            scope.$watch(clear, function () {
                if (clear == true) {
                    this.clearAll();
                }
            })

这是我的父 Controller 中更改“clear”值的函数

    $scope.clearFilters = function (clear) {

    $scope.clear = true;

    $scope.form.selected.services = [];
    $scope.form.picked.areas = [];
    $scope.form.certified.verifications = [];
    $scope.form.subscribed.subscriptions = [];
    $scope.form.OperatorBusinessUnitID = null;
    $scope.form.OperatorBusinessUnitID = null;
};

我尝试设置一个名为“clearFilter”的属性并将变量分配给它,但 watch 仍然没有触发:

            scope.$watch(attrs.clearFilter, function (value) {
                if (value == true) {
                    this.clearAll();
                }
            });

                            <checkbox-picklist data-item-id="'servicesPicklist'"
                                           data-search-placeholder="Search Services"
                                           data-list="services"
                                           data-title="Service(s)"
                                           data-name-prop="'vchDescription'"
                                           data-callback-fn="addService(object)"
                                           call-more="loadMoreServices()"
                                           clear-filter="clear">

                        </checkbox-picklist>

我不确定我是否正确调用了该函数。上面的 scope.$parent 确实从父作用域获取变量的初始值,但一旦改变,它就永远不会更新。

EDIT:What I have discovered is the normal scope.$watch('clear', function...) is not working it seems because the directive is in "ssq.shared" module which is injected in my my Main Module "myModule" (see below), so even though the page the directive is on uses my 'GeneralSearchCtrl', I cannot get the watch to work on the variable located in 'GeneralSearchCtrl'. If I use scope.$parent.clear I can see the value of the variable, but I cannot seem to set a watch on it.

我的模块注入(inject)代码:

var app = angular.module('myModule', ['ui.bootstrap', 'checklist-model', 'ssq.shared', 'ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.pagination', 'ui.grid.selection', 'ui.grid.exporter', 'ui.grid.autoResize', 'ui.router', 'cgBusy', 'ui.mask', 'ngFileUpload', 'ngSanitize']);

指令所在的页面使用:

<div ng-app="myModule" ng-controller="GeneralSearchCtrl">

我无法监视位于 GeneralSearchCtrl 中的变量。

非常感谢任何帮助!!!!

最佳答案

添加 $scope 值的监视并调用该函数,

scope.$watch('clear', function(newValue, oldValue) {
if (newValue) {
  this.clearAll();
}
});

关于javascript - 当父范围变量更改时调用指令中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39645693/

相关文章:

javascript - 这是用ajax处理发布变量的好方法吗

javascript - AngularJS 需要执行一个函数两次才能显示结果

javascript - 如何将 <div> 替换为另一个 <div> 单击 div 中的链接

javascript - angularjs http 只工作一次

javascript - 如何将获取到的记录一条一条的显示出来

Javascript - 解析文件创建和保存......但不是类中的对象......它去哪里了?

javascript - Gruntjs angularjs cordova 图像未加载

javascript - 考虑 undefined variable

javascript - jQuery 查找返回空字符串

javascript - ng-grid展开和折叠行