如何检查AngularJS中是否指定了伪指令的方法参数

如何检查AngularJS中是否指定了伪指令的方法参数

本文介绍了如何检查AngularJS中是否指定了伪指令的方法参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含按钮的自定义指令。此按钮从callback属性指定的父作用域调用方法。

 <!DOCTYPE html& 
< html ng-app =app>
< head>
< title>简单指令< / title>

< script src =js / lib / angular / angular.js>< / script>

< script type =text / javascript>
var app = angular.module('app',[]);

app.controller('TestController',function($ scope){

$ scope.doSomething = function(param){
alert :'+ param);
}
})

app.directive('myDirective',function(){
var ret = {
restrict :'E',
scope:{
user:'@',
callback:'&'//绑定范围内的函数
},
template:'< div> Hello {{user}}< button ng-show =hasCallback()ng-click =callback({userData:user})> Callback< / button>',
controller:function($ scope){
$ scope.hasCallback2 = function(){
var t = typeof $ scope.callback;
return t =='function';
}

$ scope.hasCallback = function(){
return angular.isDefined($ scope.callback);
}
}
};
return ret;
});

< / script>
< / head>

< body ng-controller =TestController>

< my-directive user =catcallback =doSomething(userData)>< / my-directive>
< my-directive user =dogcallback =doSomething(userData)>< / my-directive>
< my-directive user =pig>< / my-directive>

< / body>

< / html>

我的问题是:



我控制按钮内部模板的可见性?如果回调属性没有在自定义标签中指定,我想隐藏它(见第3个my-directive标签)。
当我检查typeof的回调,我总是得到'function'和angular.isDefined(...)也返回true。

解决方案

如果未设置属性,则使用& ;?返回undefined。



'&'=始终定义回调函数。 >

'& ;?'=回调函数仅在html模板中定义了属性时定义。

 code> bindToController:{
callback:'& ;?'
},
controller:function(){
if(this.callback === undefined){
//未定义属性callback
}
}

注意:在Angular 1.4.8中工作。我不知道它是否在旧版本中工作。


I've created a custom directive which contains a button. This button calls a method from parent scope specified by 'callback' attribute.

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title>Simple directive</title>

    <script src="js/lib/angular/angular.js"></script>

    <script type="text/javascript">
        var app = angular.module('app', []);

        app.controller('TestController', function($scope) {

            $scope.doSomething = function(param) {
                alert('Something called with: ' + param);
            }
        })

        app.directive('myDirective', function() {
            var ret = {
                restrict: 'E',
                scope: {
                    user: '@',
                    callback: '&'       // bound a function from the scope
                },
                template: '<div>Hello {{user}}<button ng-show="hasCallback()" ng-click="callback({userData: user})">Callback</button>',
                controller: function($scope) {
                    $scope.hasCallback2 = function() {
                        var t = typeof $scope.callback;
                        return t == 'function';
                    }

                    $scope.hasCallback = function() {
                        return angular.isDefined($scope.callback);
                    }
                }
            };
            return ret;
        });

    </script>
</head>

<body ng-controller="TestController">

<my-directive user="cat" callback="doSomething(userData)"></my-directive>
<my-directive user="dog" callback="doSomething(userData)"></my-directive>
<my-directive user="pig"></my-directive>

</body>

</html>

My question is:

How can I control visibility of button inside template? I'd like to hide it if callback attribute not specified in custom tag (see 3rd my-directive tag).When I check typeof of callback, I always get 'function' and angular.isDefined(...) also returns true.

解决方案

Using '&?' returns undefined if the attribute has not been set.

'&' = callback function is defined always.

'&?' = callback function is defined only when attribute is defined in html template.

bindToController: {
    callback: '&?'
},
controller: function() {
    if (this.callback === undefined) {
        // attribute "callback" was not defined
    }
}

Note: Works in Angular 1.4.8. I'm not sure if it works in older versions.

这篇关于如何检查AngularJS中是否指定了伪指令的方法参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:42