在app.config中注入服务?

时间:2016-05-05 19:50:07

标签: angularjs node.js angularjs-injector angularjs-config

我想在app.config中注入一个服务吗?我想在app.config中注入一个服务吗?我想在app.config中注入一个服务吗?

app.js

  'use strict';

angular.module('crud', [
  'ngRoute',
  'angular-jwt',
  'ngSails',
  'ngMessages',
  'ngResource'


])
  .config(function ($httpProvider,$routeProvider, $locationProvider,$sailsProvider,jwtInterceptorProvider,User) {


  //$httpProvider.interceptors.push('jwtInterceptor');


     //console.log($sailsProvider);
    $routeProvider
      .otherwise({
        redirectTo: '/'
      });

    $locationProvider.html5Mode(true);

  });

Serviceuser.js

'use strict';

angular.module('crud').service('User', function ($sails) {

    //console.log($sails);
    return {

        signup:  function (data) {
            return $sails.post('/api/user',data);       
        }
    } 

});

2 个答案:

答案 0 :(得分:0)

您可以直接从docs

开始
Registering a Service with $provide
You can also register services via the $provide service inside of a module 's config function:


angular
  .module('myModule', [])
  .config(['$provide ',
    function($provide) {
      $provide.factory('serviceId ', function() {
        var shinyNewServiceInstance;
        // factory function body that constructs shinyNewServiceInstance
        return shinyNewServiceInstance;
      });
    }
  ]);


This technique is often used in unit tests to mock out a service'
s dependencies.

希望这会有所帮助。

(function() {
  'use strict';

  angular
    .module('example.app', [])
    .config(['$provide',
      function($provide) {
        $provide.factory('serviceId', function() {
          var shinyNewServiceInstance;
          // factory function body that constructs shinyNewServiceInstance
          return shinyNewServiceInstance;
        });
      }
    ])
    .controller('ExampleController', ExampleController)
    .service('exampleService', exampleService);

  exampleService.$inject = ['$http'];

  function ExampleController(exampleService) {
    var vm = this;

    vm.update = function(person, index) {
      exampleService.updatePeople(person).then(function(response) {
        vm.persons = response;
      }, function(reason) {
        console.log(reason);
      });
    };
  }


  // good practice to use uppercase variable for URL, to denote constant.    
  //this part should be done in a service

  function exampleService($http) {
    var URL = 'https://beta.test.com/auth/authenticate/',
      data = {},
      service = {
        updatePeople: updatePeople
      };

    return service;

    function updatePeople(person) {
      //person would be update of person.
      return $http
        .post(URL, person)
        .then(function(response) {
          return response.data;
        }, function(response) {
          return response;
        });
    }
  }
})();

答案 1 :(得分:-1)

你可以使用:

angular.module('app', ["ui.router"])
.config(function config ($stateProvider){
    $stateProvider.state("index", {
      url:"",
      controller: "FirstCtrl as first",
      templateUrl: "first.html"
    })
    .state("second", {
      url:"/second",
      controller:"SecondCtrl as second",
      templateuRL:"second.html"
    })
})

这是plunker

的完整工作示例