我需要從指令中觀察屬性,並在值爲真時切換函數,第一次單擊它時會起作用,但是之後需要重置評估ATTRIBUTE/Attribute模型的值,這是我無法弄清楚的部分。股之如何在不使用範圍聲明的情況下切換指令
DEMO:https://jsfiddle.net/DG24c/200/
期望得到的結果將是在我的演示中EXTERNAL ACCESS
按鈕始終觸發警報的方法。
模板:
<div ng-controller="otherController">
<button interactive show-when="toggled">Show Directive Alert</button>
<button ng-click="toggled = true" >External access</button>
</div>
的Javascript:
myApp.controller("otherController",function($scope){
$scope.toggled = false;
});
myApp.directive('interactive', function($parse) {
return {
restrict : 'A',
// scope : true || {} cannot be used
link : function(scope, element,attrs) {
element.on('click', function() {
show();
});
scope.$watch(attrs.showWhen, function(newValue,o) {
if (newValue) {
// the value is true, but now I need to reset
// the value on TOGGLED back to false here, so that the next
// time the button clicks, it will show the alert
show();
// tried attrs.$set('showWhen', false);
// tried var showAttr = $parse(attrs.showWhen)();
// showAttr = false;
}
});
function show() {
alert('showing!');
}
}
};
})
實際上@jwpfox這也幫我解決這個問題,我已經編輯自己的答案闡述 –