Check existence of attribute in AngularJs Directive

前端 未结 4 397
难免孤独
难免孤独 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:22

    To Check for attributes when using angular 1.5+ components you can use the $postLink lifecycle hook and the $element service like this:

    constructor(private $element) {}
    
    $postLink() {
      if(!this.$element.attr('attr-name')){
        // do something
      }
    }
    

提交回复
热议问题