角指令回调 [英] Angular Directive Callback
本文介绍了角指令回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过对标签的参数发送呼叫回一个指令,然后调用该方法在适当的时候该指令中。例如,当一个按钮被点击的调用父控制器上的方法。
我有它的一个简单的 plunker 不工作
HTML文件:
<机身NG控制器=ParentController>
< H1>方法的参数和LT; / H1>
< H3>打开控制台查看输出< / H3 GT&;
<你好世界onLoadCallback =myCallBack函数(ARG1)>< /你好世界>
< /身体GT;
JavaScript文件:
VAR应用= angular.module(应用程序,[]);功能ParentController($范围){
$ scope.myCallback =功能(VAR1){
的console.log(myCallBack函数,VAR1);
}
}
app.directive('的helloWorld',函数(){
返回{
限制:AE,
模板:'< H3>的Hello World!< / H3和GT;,
范围:{
onLoadCallback:'和;'
},
链接:功能(范围,元素,ATTRS,dateTimeController){
的console.log('链接指令,而不是调用回调')
scope.onLoadCallback('跆拳道'); }
};
});
解决方案
整蛊刁钻的角度,在HTML声明的说法的时候,它需要使用蛇的情况下,而不是驼峰匹配。
作品:
<你好世界载回调=myCallBack函数(ARG1)>< /你好世界>
不工作:
<你好世界onLoadCallback =myCallBack函数(ARG1)>< /你好世界>
I would like to send a call back into a directive via a parameter on the tag, and then call that method when appropriate inside the directive. For example, when a button was clicked call a method on the parent controller.
I have a simple plunker of it not working
html file:
<body ng-controller="ParentController">
<h1> Method Arguments </h1>
<h3> open console to view output</h3>
<hello-world onLoadCallback="myCallback(arg1)"></hello-world>
</body>
javascript file:
var app = angular.module("app",[]);
function ParentController($scope) {
$scope.myCallback = function(var1){
console.log("myCallback", var1);
}
}
app.directive('helloWorld', function() {
return {
restrict: 'AE',
template: '<h3>Hello World!!</h3>',
scope:{
onLoadCallback: '&'
},
link: function(scope, element, attrs, dateTimeController) {
console.log('linked directive, not calling callback')
scope.onLoadCallback('wtf');
}
};
});
解决方案
Tricky tricky angular, when declaring the argument in the HTML, it needs to use snake case, instead of camelcase to match.
Works:
<hello-world on-load-callback="myCallback(arg1)"></hello-world>
Doesn't Work:
<hello-world onLoadCallback="myCallback(arg1)"></hello-world>
这篇关于角指令回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文