首页 > Web开发 > 详细

Angularjs service vs factory vs provider

时间:2016-01-20 12:46:01      阅读:196      评论:0      收藏:0      [点我收藏+]

Angular provides us with three ways to create and register our own service.

1) Factory

2) Service

3) Provider

TL;DR

1) When you’re using a Factory you create an object, add properties to it, then return that same object. When you pass this service into your controller, those properties on the object will now be available in that controller through your factory.

技术分享

 

2) When you’re using Service, it’s instantiated with the ‘new’ keyword. Because of that, you’ll add properties to ‘this’ and the service will return ‘this’. When you pass the service into your controller, those properties on ‘this’ will now be available on that controller through your service.

技术分享

 

3) Providers are the only service you can pass into your .config() function. Use a provider when you want to provide module-wide configuration for your service object before making it available.

技术分享

 

NON TL;DR

1) Factory

技术分享

 

技术分享

Angularjs service vs factory vs provider

原文:http://www.cnblogs.com/yk00/p/5144430.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!