gpt4 book ai didi

javascript - $watch 不是函数

转载 作者:行者123 更新时间:2023-11-30 00:05:32 25 4
gpt4 key购买 nike

我有一个看起来像这样的 Angular Directive(指令)

    app.directive('paymentsTable', ['$watch', function($watch) {
return {
replace: true,
restrict: 'EACM',
templateUrl: '../views/paymentTable.html',
link: function(elem, attr, scope) {

console.log(elem.$parent.payments); // array

scope.$watch(function(elem) { return elem.$parent.payments }, function(value, oldValue) {

});
}
};
}]);

它给了我

angular.js:13920Error: [$injector:unpr]

当我这样重写第一行时

app.directive('paymentsTable', [ function() {

它给了我另一个错误

angular.js:13920TypeError: o.$watch is not a function

我也使用 uglify。所以,我的问题是:这是怎么回事?

最佳答案

$watch 函数是在 link 方法中传递给您的 scope 的一部分,因此无需注入(inject)它.出现第二个错误的原因是 link 参数的顺序。像这样尝试:

 app.directive('paymentsTable', function() { // no need for injection
return {
replace: true,
restrict: 'EACM',
templateUrl: '../views/paymentTable.html',
link: function(scope, element, attrs) { // The correct arguments order

console.log(elem.$parent.payments);

scope.$watch(function(elem) { return elem.$parent.payments }, function(value, oldValue) {

});
}
};
});

关于javascript - $watch 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38682101/

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