码迷,mamicode.com
首页 > 其他好文 > 详细

在ANGULAR的SERVICE中,哪种才是最基本的实现?(Provider)

时间:2016-06-20 15:30:58      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

今天刚好看到这一节。

节选一下,稍后,实操完成之后,会补上所有代码

Sometimes, it might be interesting to create configurable services. They are called providers, and despite being more complex to create, they can be configured before
being available to be injected inside other components.
While the factory works by returning an object and the service with the constructor function, the provider relies on the $get function to expose its
behavior. This way, everything returned by this function becomes available through the dependency injection.
In the following code, we refactored our service to be implemented by a provider. Inside the $get function, the calculateTicket method is being returned and will be
accessible externally。

parking.provider("parkingService", function (parkingConfig) {
var _parkingRate = parkingConfig.parkingRate;
var _calculateTicket = function (car) {
var departHour = new Date().getHours();
var entranceHour = car.entrance.getHours();
var parkingPeriod = departHour – entranceHour;
var parkingPrice = parkingPeriod * _parkingRate;
return {
period: parkingPeriod,
price: parkingPrice
};
};
this.setParkingRate = function (rate) {
_parkingRate = rate;
};
this.$get = function () {
return {
calculateTicket: _calculateTicket
};
};
});

 

在ANGULAR的SERVICE中,哪种才是最基本的实现?(Provider)

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5600354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!