我如何在不使用范围声明的情况下切换指令
How can i toggle directive without using scope declaration
我需要从指令中观察一个属性,并在值为真时切换一个函数,这是你第一次点击它,但我需要重置attribute/attribute模型的值,一旦它被评估,这是我无法弄清楚的部分。
DEMO OF ISSUE: https://jsfiddle.net/DG24c/200/
期望的结果是,在我的演示中,EXTERNAL ACCESS
按钮总是触发alert方法。
模板:
<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!');
}
}
};
})
试试这个:
$parse(attrs.showWhen + ' = false')(scope);
演示:https://jsfiddle.net/DG24c/204/
Javascript:myApp.directive('interactive', function($parse) {
return {
restrict : 'A',
link : function(scope, element,attrs) {
element.on('click', function() {
show();
});
scope.$watch(function(inboundScope) {
// gets the value of the attribute
var interactive = $parse(attrs.showWhen)(inboundScope);
if (!interactive) return false; // no need to continue if the value is already false
$parse(attrs.showWhen + ' = false')(inboundScope); // updates parent scopes value back to false
return interactive;
}, function(newValue) {
if (newValue) show();
});
function show() {
alert('showing!' + attrs.showWhen);
}
}
};
});
HTML: <div ng-app="myApp">
<div ng-controller="myCtrl as $ctrl">
<button interactive show-when="$ctrl.toggled">Show Directive Alert</button>
<button ng-click="$ctrl.toggled = true" >External access</button>
<pre>{{$ctrl.toggled | json}}</pre>
<button interactive show-when="$ctrl.toggled2">Show Directive Alert2</button>
<button ng-click="$ctrl.toggled2 = true" >External access2</button>
<pre>{{$ctrl.toggled2 | json}}</pre>
</div>
</div>
你不应该使用这样的属性,你需要使用作用域。不太确定你的评论// scope : true || {} cannot be used
是什么意思…你为什么不能?
如果您实际使用数据绑定与作用域,而不是仅仅查看属性,它工作得很好:https://jsfiddle.net/DG24c/203/
myApp.directive('interactive', function($parse) {
return {
restrict : 'A',
scope: {
showWhen: '='
},
link : function(scope, element) {
element.on('click', function() {
show();
});
scope.$watch('showWhen', function(v,o) {
if (v) {
scope.showWhen = false;
show();
}
});
function show() {
alert('showing!');
}
}
};
})
相关文章:
- AngularJs,在没有隔离的情况下将指令属性添加到作用域
- 在不使用隔离作用域的情况下执行函数的角度指令
- JavaScript 变量在没有指令的情况下写入其他变量
- 如何使 AngularJS 指令在不隔离作用域的情况下清楚地定义其依赖关系
- $animate.removeClass 在没有 $evalAsync inside 指令的情况下不起作用
- 在我的情况下,如何在指令中设置$emit
- 在不注入$rootScope的情况下对指令中的角度事件做出反应
- AngularJS指令在元素具有绑定值的情况下不起作用
- Angular JS自定义指令在没有ng-app指令的情况下不起作用
- 如何在不覆盖父范围的情况下将参数传递给指令
- 如何在不使用指令的情况下操作ng-if内部的元素
- 在没有自己的控制器的情况下使用指令作用域何时合适
- 你能在没有指令的情况下在ngModel上动态呈现HTML吗
- 在没有指令的情况下将模板编译成angular字符串
- 在不使用隔离范围angularjs自定义指令的情况下获取属性对象
- 如何在指令的情况下更改下拉列表的默认文本,并对指令应用所需的验证
- 我如何在不使用范围声明的情况下切换指令
- 在我的情况下,如何将数据从指令传递到父范围
- 如何在不发送HTML代码的情况下共享AngularJS指令
- 点击Shift键可以在不使用angular指令的情况下为对象添加值