这是我的代码
var directive = {
restrict: 'E',
templateUrl: '/static/myTemplate.html',
scope: {
},
controller: ["$scope", controllerFunc],
controllerAs: 'multiCheckboxCtrl',
bindToController: true,
link: linkFunc,
};
return directive;
function controllerFunc($scope) {
// $watch List
/**
* Event handler for searchText change
*/
$scope.$watch(angular.bind(this, function () {
return $scope.multiCheckboxCtrl.clickedElsewhere.value;
}), function (newVal) {
if (newVal !== undefined && newVal !== "") {
console.log(newVal);
console.log("I am here");
}
});
}
function linkFunc(scope, element, attr){
// Detect Element Click Logic
scope.multiCheckboxCtrl.clickedElsewhere = {value: false};
$document.on('click', function(){
scope.multiCheckboxCtrl.clickedElsewhere.value = false;
console.log(scope.multiCheckboxCtrl.clickedElsewhere);
});
element.on('click', function(){
event.stopPropagation();
scope.multiCheckboxCtrl.clickedElsewhere.value = true;
console.log(scope.multiCheckboxCtrl.clickedElsewhere);
});
// End Detect Element Click Logic
}
linkFunc 基本上决定了指令元素是否被单击。
我验证了只要单击指令元素,它就会控制台为 true,而当单击指令元素之外的任何元素时,它会控制台为 false。
但是 Controller 中的 $watch
似乎没有捕获更改。
谁能告诉我出了什么问题
谢谢
最佳答案
Angular 不知道事件监听器中的对象属性发生了更改。
如documentation 说:
$apply()
is used to execute an expression in angular from outside of the angular framework. (For example from browserDOM events
,setTimeout
,XHR
or third party libraries). Because we are calling into the angular framework we need to perform proper scope life cycle of exception handling, executing watches.
jsfiddle 上的实例.
angular.module('ExampleApp', [])
.controller('ExampleController', function($scope) {
})
.directive('clickDirective', function() {
var directive = {
restrict: 'E',
template: '<div>click me</div>',
scope: {},
controller: ["$scope", controllerFunc],
controllerAs: 'multiCheckboxCtrl',
bindToController: true,
link: linkFunc,
};
return directive;
function controllerFunc($scope) {
// $watch List
/**
* Event handler for searchText change
*/
$scope.$watch(function() {
return $scope.multiCheckboxCtrl.clickedElsewhere.value;
}, function(newVal) {
if (newVal !== undefined && newVal !== "") {
console.log(newVal);
console.log("I am here");
}
});
}
function linkFunc(scope, element, attr) {
// Detect Element Click Logic
scope.multiCheckboxCtrl.clickedElsewhere = {
value: false
};
angular.element(document).on('click', function() {
scope.multiCheckboxCtrl.clickedElsewhere.value = false;
console.log(scope.multiCheckboxCtrl.clickedElsewhere);
scope.$apply();
});
element.on('click', function() {
event.stopPropagation();
scope.multiCheckboxCtrl.clickedElsewhere.value = true;
console.log(scope.multiCheckboxCtrl.clickedElsewhere);
scope.$apply();
});
// End Detect Element Click Logic
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="ExampleApp">
<div ng-controller="ExampleController" id="ExampleController">
<click-directive></click-directive>
<div>
click elsewhere
</div>
</div>
</div>
关于javascript - 当指令 Controller 使用 Angular 读取它时,指令链接中的范围变量不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759569/