Javascript 尝试在范围隔离指令中运行绑定方法
为什么这不起作用Javascript 尝试在范围隔离指令中运行绑定方法,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,为什么这不起作用 index.html <div ng-controller="mainController"> <sun external-fct="changeValue(internalValue)"></sun> </div> directions.js controllers.controller('mainController', ['$scope', function ($scope) { $scope.chan
index.html
<div ng-controller="mainController">
<sun external-fct="changeValue(internalValue)"></sun>
</div>
directions.js
controllers.controller('mainController', ['$scope', function ($scope) {
$scope.changeValue = function (newValue) {
$scope.value = newValue;
}
$scope.$watch('value', function (n, o) {
if (n !== o)
alert('the value has changed to ' + n);
});
}]);
directives.directive('sun', function () { return {
scope: {
fct: '&externalFct'
},
template: '<button>click me</button>',
link: function (scope, element, attrs) {
element.on('click', function (e) {
scope.fct({
internalValue : 'test'
})
});
}
}});
指令('sun',函数(){return{
范围:{
fct:“&外部fct”
},
模板:“单击我”,
链接:函数(范围、元素、属性){
元素上('click',函数(e){
scope.fct({
内在价值:“测试”
})
});
}
}});
当我单击
时,指令的外部作用域不会$watch
更改的值 WWHOOPS!我只是忘了在单击时运行$digest()