码迷,mamicode.com
首页 > Web开发 > 详细

[AngularJS] 'require' prop in Directive or Component

时间:2016-08-14 07:17:54      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:

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

[AngularJS] 'require' prop in Directive or Component

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5769126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!