gpt4 book ai didi

两种方式绑定(bind)的 AngularJS 指令 $scope.$watch 不起作用

转载 作者:行者123 更新时间:2023-12-02 00:57:25 25 4
gpt4 key购买 nike

<分区>

我正在尝试检测 AngularJS (1.7.2) 指令中双向绑定(bind)范围的变化。

// pagination.js (directive)
app.directive("pagination", ($rootScope) => {
return {
templateUrl: "/shared/filters/pagination/pagination.html",
scope: {
filter: "="
},
link: function($scope, element, attrs) {

$scope.$watch("filter", value => {
console.log(value); // first time works, later it's undefined
});

}
}
});

// parent.html
<pagination filter="filter"></pagination>

// parent.js
$scope.filter = {
status: "All"
};

$scope.filter 正在通过函数进行更改,该函数也位于 parent.js 文件中:

$scope.someFunction = () => {
$scope.filter.status = "Pending";
// this should fire the $scope.$watch event in the pagination.js (directive)
// however this doesn't get applied
};

如何让作用域监听来自其他指令的变化?

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