I want to inject a service into app.config any idea please ?I want to inject a service into app.config any idea please ?I want to inject a service into app.config any idea please ?
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);
}
}
});
Here you go straight from the 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.
Hope this helps.
(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;
});
}
}
})();
you can use like:
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"
})
})
here is the full working example with plunker
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.