How to check if a method argument of a directive is specified in AngularJS?

前端 未结 2 918
北荒
北荒 2020-12-02 18:31

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



        
2条回答
  •  悲&欢浪女
    2020-12-02 18:50

    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.

提交回复
热议问题