Check existence of attribute in AngularJs Directive

前端 未结 4 399
难免孤独
难免孤独 2021-02-05 00:33

Is is possible to check whether a given attribute is present in a directive, ideally using isolate scope or in a worst case scenario the attributes object.

With a direct

4条回答
  •  不思量自难忘°
    2021-02-05 01:20

    Since the attrs value is type of javascript object. To check attribute existence we can also using hasOwnProperty() method instead in keyword.

    So, it could be :

    link: function(scope, element, attrs) {
      var is_key_exist = attrs.hasOwnProperty('status');//Will return true if exist
    }
    

    You can read further the difference between in keyword and hasOwnProperty() method at this link

提交回复
热议问题