首页 > Web开发 > 详细

[AngularJS] 'require' prop in Directive or Component

时间:2016-08-14 07:16:18      阅读:189      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!