2016-05-05 4 views
0

Ich möchte einen Dienst in app.config injizieren jede Idee bitte? Ich möchte einen Dienst in app.config injizieren jede Idee bitte? Ich möchte einen Dienst in app.config eine Idee bitte injizieren?Inject Dienst in 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);  
     } 
    } 

}); 
+0

sehen diese http://stackoverflow.com/questions/15937267/inject-service-in- app-config –

Antwort

0

Hier gehen Sie direkt aus dem 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. 

Hoffnung, das hilft.

(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; 
     }); 
    } 
    } 
})(); 
+0

können Sie ein Beispiel geben? – letseasy

+0

@letseasy Update-Code wird es bald hier haben. – alphapilgrim

-1

Sie können wie verwenden:

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" 
    }) 
}) 

hier ist die volle Arbeitsbeispiel mit plunker