zoukankan      html  css  js  c++  java
  • [AngularJS] 'require' prop in Directive or Component

    When use 'require', recommend to add some error check, for example:

    class ChildCtrl {
    
       constructor(){
           
           // Get prop from parent ctrl
           if(this.parentCtrl){
                this.childProp = this.parentCtrl.prop;
           }
       }
    }
    
    app.directive('someDirective', () => {
    
       return {
    
            require: {
               'parentCtrl': '?^^'
           },
           controller: ChildCtrl,
           controllerAs: 'vm',
           bindToController: true,
           scope: {},
           template: require('./child.tpl.html')
       }
    })

    We add a if() to check whether this is parent controller, if it is then we continue do something, if not, just ignore. This can prevent if there is no parent controller, we won't get undefined error

  • 相关阅读:
    GDUFE ACM-1050
    hdu-2020
    hdu-2055
    hdu-2734
    GDUFE ACM-1145
    GDUFE ACM-1127
    GDUFE ACM-1126
    GDUFE ACM-1125
    GDUFE ACM-1124
    GDUFE ACM-1123
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5769126.html
Copyright © 2011-2022 走看看