AngularJS UI Router using resolved dependency in factory / service

后端 未结 3 1304
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-09 15:04

I have a UI Router defined something like this (trimmed for simplicity):

    $stateProvider
        .state(\'someState\', {
            resolve: {
                       


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-09 15:29

    You should review the angular docs on dependency injection:

    • Components such as services, directives, filters, and animations are defined by an injectable factory method or constructor function. These components can be injected with "service" and "value" components as dependencies.
    • Controllers are defined by a constructor function, which can be injected with any of the "service" and "value" components as dependencies, but they can also be provided with special dependencies. See Controllers below for a list of these special dependencies.
    • The run method accepts a function, which can be injected with "service", "value" and "constant" components as dependencies. Note that you cannot inject "providers" into run blocks.
    • The config method accepts a function, which can be injected with "provider" and "constant" components as dependencies. Note that you cannot inject "service" or "value" components into configuration.

    So each type of angular component has its own list of acceptable components to inject. Since services are singletons, it really wouldn't make any sense to inject a value as part of a resolve. If you had two separate places on your page that used a service with different resolves, the outcome would be indeterminate. It would make no more sense than injecting $scope into your service. It makes sense for controllers because the controller is responsible for the same area of the page that is being resolved.

    If your someService needs to use the data in model, it should have a function that takes the data as a parameter and your controller should pass it.

提交回复
热议问题