admin管理员组文章数量:1319025
Can you please explain the use use of data services vs the use of $rootScope events.
I have a list of say branches which provides edit functionality. On click of edit button I am broadcasting an event with root scope using
$rootScope.$broadcast('EditBranch', branchID);
This EditBranch
event is captured by edit/create controller which fetches the branch details and renders it in a proper edit format.
Other function is I am adding a new branch and I want it to be listed in existing branch list as soon as it is added. the code used is as follows
$rootScope.$broadcast('AddBranch', branchData); //in create controller
$scope.$on('AddBranch', function(e, branchData){ //in listing controller
$scope.branches.push(branchData);
});
Is it right to use $rootScope this way. Or should I create a shredService
for sharing branch data after I create it.
Can you please explain the use use of data services vs the use of $rootScope events.
I have a list of say branches which provides edit functionality. On click of edit button I am broadcasting an event with root scope using
$rootScope.$broadcast('EditBranch', branchID);
This EditBranch
event is captured by edit/create controller which fetches the branch details and renders it in a proper edit format.
Other function is I am adding a new branch and I want it to be listed in existing branch list as soon as it is added. the code used is as follows
$rootScope.$broadcast('AddBranch', branchData); //in create controller
$scope.$on('AddBranch', function(e, branchData){ //in listing controller
$scope.branches.push(branchData);
});
Is it right to use $rootScope this way. Or should I create a shredService
for sharing branch data after I create it.
2 Answers
Reset to default 5It may not be easy to say if one approach is better than the other. However, I would use a shared service in this case since it is all about manipulating the same data, branch
(I suppose). $broadcast
and $on
are more appropriate in situations where different "independent" ponents of your application could be listening for an event and each of these ponents could response differently when the event occurs.
Service is a Singleton and you can inject to any controller and invoke getter/setter service values in a controller's scope.
I think with Services you have better control over data and might make code clear.
本文标签: javascriptAngularJS Data service vs rootScope eventsStack Overflow
版权声明:本文标题:javascript - AngularJS Data service vs $rootScope events - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742050368a2418033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论