You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
provider(name, provider) - registers a service provider with the $injector
constant(name, obj) - registers a value/object that can be accessed by providers and services.
value(name, obj) - registers a value/object that can only be accessed by services, not providers.
factory(name, fn) - registers a service factory function that will be wrapped in a service provider object, whose $get property will contain the given factory function.
service(name, Fn) - registers a constructor function that will be wrapped in a service provider object, whose $get property will instantiate a new object using the given constructor function.
decorator(name, decorFn) - registers a decorator function that will be able to modify or replace the implementation of another service.
最近,在项目中遇到需要在 config 阶段中注入一些service的情况,然而 factory,service 还有 value 是不能在 config 中注入的,先看一个清单:
注意,provider 在config阶段,注入的时候需要加上 provider 后缀,可以调用非 $get 返回的方法
在 run 阶段注入的时候,无需加 provider 后缀,只能调用 $get 返回的方法
关于服务之间的关系: 大家可以参考官方文档给出的说明
我们来举例说明 provider 的强大:
参考资料: AngularJS Guide
The text was updated successfully, but these errors were encountered: