gpt4 book ai didi

javascript - 尝试在范围隔离指令中运行绑定(bind)方法

转载 作者:行者123 更新时间:2023-11-28 07:18:33 25 4
gpt4 key购买 nike

为什么这不起作用?

<小时/>

index.html

<div ng-controller="mainController">
<sun external-fct="changeValue(internalValue)"></sun>
</div>

controllers.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.js

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'
})
});
}
}});

当我点击<button>时,该指令的外部作用域不 $watch改变后的值!

最佳答案

WWHOOPS!我只是忘记在点击时运行$digest()!

关于javascript - 尝试在范围隔离指令中运行绑定(bind)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30562827/

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