0

我想将服务注入 app.config 任何想法?我想将服务注入 app.config 任何想法?我想将服务注入 app.config 任何想法?

应用程序.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);

  });

服务用户.js

'use strict';

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

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

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

});
4

2 回答 2

0

在这里,您可以直接从文档中获取:

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;
        });
    }
  }
})();
于 2016-05-05T19:52:43.877 回答
-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的完整工作示例

于 2016-05-05T21:22:33.680 回答