[英]Watch on controller scope from directive link function
I am working on an AngularJs application and I have a confusing issue here when I am trying to watch on controller scope property within link function of a directive 我正在使用AngularJs应用程序,当我尝试观察指令链接功能内的控制器作用域属性时,这里有一个令人困惑的问题
Here is minimal example of the issue 这是问题的最小示例
HTML HTML
<div fc-loading="filterBox"></div>
controller 控制者
angular.module('myApp').controller('myctrl', function ($scope) {
$scope.filterBox = false;
setInterval(function () {
$scope.filterBox = !$scope.filterBox;
},2000)
}
Directive 指示
angular.module('myApp').directive('fcLoading', function () {
return{
scope:{
fcLoading:'='
},
link: function (scope,ele,attrs) {
scope.$watch(scope[attrs.fcLoading],function(newValue, oldValue){
console.log(newValue)
}
}
Thanks in advance! 提前致谢!
AngularJs Directive scope '=' method lets you to have 2 ways binding inside your directive, and you can only watch your directives scope variable .like this AngularJs Directive范围'='方法使您可以在指令内部进行两种绑定 ,并且只能观看指令的范围变量。
scope.$watch('fcLoading', function(newValue, oldValue){
console.log(newValue);
}, true)
By the way call $scope.$apply() inside setInterval function or use $interval 通过在setInterval函数中调用$ scope。$ apply()或使用$ interval
You should be able to just watch the value that you're putting on the scope here: 您应该能够仅在此处观察将要放在示波器上的值:
scope:{
fcLoading:'='
},
like this in your link function: 在您的链接函数中是这样的:
scope.$watch(scope.fcLoading, function(newValue, oldValue){
console.log(newValue);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.