Angular-Parent/Child Controller Communication

参考:Parent/Child Controller Communication

1.简单粗暴的$scope 

ng的$scope采用继承机制,所以,childController中复制了parentController的所有属性和方法。并且,childController can override parentController's methods or properties. 所以,最简单的方式:子类通过$parent去修改父类的属性或方法。

优势
子类与父类强耦合 简单+直接,操作方便
子类可能会错误的修改父类的状态 当子类只与一个父类强关联时,可以很方便的使用
子类难以被复用(因为与父类强耦合)  

 

 

 


2.PubSub 

优势
产生更多的事件引用 解除父子耦合(angular作为中间的消息传递者)
无法保证事件处理器的先后顺序  支持One-to-many, many-to-many,  many-to-one消息传递形式
会产生事件循环调用的潜在bug  

 

 

 

 

备注:I highly advise not to use $rootScope.$broadcast + $scope.$on but rather $rootScope.$emit$rootScope.$on. The former can cause serious performance problems as raised by @numan. That is because the event will bubble down through all scopes.

3.UI Router

4.Service

 参考:What's the correct way to communicate between controllers in AngularJS?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值