大佬教程收集整理的这篇文章主要介绍了javascript – Angular Directive绑定函数带有&不传递参数给控制器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
一旦用户选择文件/文件夹,Box文件选择器就可以设置回调函数,如下所示:
var boxSELEct = new BoxSELEct(); // Register a success callBACk handler boxSELEct.success(function(responsE) { console.log(responsE); });
我的控制器正在使用该指令,并且将成功的回调逻辑作为范围变量,我正在传递给该指令.
我创建了一个plunkr,我在嘲笑Box选择行为
调节器
.controller('myController',function($scopE) { $scope.onsuccessful = function(messagE) { alert('success! message: ' + messagE); }; })
指示
angular.module('myApp',[]) .controller('myController',function($scopE) { $scope.onsuccessful = function(messagE) { //message is undefined here alert('success! message: ' + messagE); }; }) .directive('myDirective',function() { return { reStrict: 'A',scope: { success: '&' },link: function(scope,element) { //third party allows to subscribe to success and failure functions function ThirdPartySELEct() { } ThirdPartySELEct.prototype.success = function(callBACk) { this.callBACk = callBACk; }; ThirdPartySELEct.prototype.firesuccess = function() { this.callBACk({ foo: 'bar' }); }; var myThirdPartyInstance = new ThirdPartySELEct(); myThirdPartyInstance.success(function(messagE) { //message is still defined here,but not in the controller scope.success(messagE); }); element.on('click',function() { myThirdPartyInstance.firesuccess(); }); } }; });
视图
<div ng-controller="myController"> <button my-directive success="onsuccessful(arg)">Test</button> </div>
回调函数在控制器内调用,但是参数
未定义
我使用’=’而不是’&’来解决这个问题,但我想知道为什么它不能与’&’因为它应该用于method binding
但是在您的指令中,当您调用绑定方法时,函数参数应该是一个对象,在定义函数时,该键与您在控制器中声明的参数相同.
所以在你的指令中,你必须更换:
scope.success(messagE);
通过:
scope.success({message:message.foo});
然后在您的HTML中,您必须更换:
<button my-directive success="onsuccessful(arg)">Test</button>
通过:
<button my-directive success="onsuccessful(messagE)">Test</button>
你可以看到Working Plunker
以上是大佬教程为你收集整理的javascript – Angular Directive绑定函数带有&不传递参数给控制器全部内容,希望文章能够帮你解决javascript – Angular Directive绑定函数带有&不传递参数给控制器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。