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

Angularjs service vs factory vs provider

时间:2016-01-20 12:46:36      阅读:256      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!