你如何检查是否 &变量在指令中设置 [英] How do you check if & variable is set in directive

查看:18
本文介绍了你如何检查是否 &变量在指令中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在指令中,您通过参数设置方法回调,例如:

In a directive, where you set a method callback through an arg, say:

<my-directive callback='showAlert()' />

您可以通过返回中的范围设置来检索变量:

You can retrieve the variable through the scope setting in the return:

scope: {
    callback: "&callback"
}

如果没有设置回调,例如:

If the callback is not set, eg:

<my-directive />

$scope.callback 的值仍然是:

The value of $scope.callback is still:

$scope.callback():function (locals) {
    return parentGet(scope, locals);
}

有没有什么好的方法可以检查回调是否没有设置?

Is there a good way to check that the callback was not set?

推荐答案

我能想到的一种方法是检查指令名称的属性参数,如

One way i can think off is to check the attribute parameter for the directive name like

link: function (scope, elm, attrs) {

   if(attrs.callback) {
       //this attribute has been defined.
   }

}

这篇关于你如何检查是否 &amp;变量在指令中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆