【发布时间】:2017-03-30 01:27:25
【问题描述】:
我需要查看指令中的属性,并在值为 true 时切换功能,这在您第一次单击它时有效,但我需要重置 ATTRIBUTE/Attribute 模型的值评估,这是我无法弄清楚的部分。
问题演示: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!');
}
}
};
})
【问题讨论】:
标签: javascript html angularjs scope angularjs-scope