gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:15:22 25 4
gpt4 key购买 nike

当父 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com