问题
As title I want to use a factory insisde a "resolve" app.js:
angular
.module("goHenry", ["ui.router"])
.factory("httpPost", httpPost)
.controller("MainCTRL", ["$scope", MainCTRL]);
function MainCTRL($scope, httpPost){
this.nameApp = "CiaoMiao";
console.log($scope.children, httpPost);
}
function httpPost($http, $q){
return {
get: function() {
var deferred = $q.defer();
$http.post.apply(null, arguments)
.success(deferred.resolve)
.error(deferred.resolve);
return deferred.promise;
}
}//RETURN
}
routers.js:
var httpConfig = {headers:{ "Content-Type" : "application/x-www-form-urlencoded" }};
var config = ["$urlRouterProvider", "$stateProvider", function($urlRouterProvider, $stateProvider){
$urlRouterProvider.otherwise("/");
$stateProvider
.state("multi",{
url: "/multi",
"viewA@multi": {
templateUrl: "app/templates/login.htm",
controller: ["$scope", "getChildrenNumber", function($scope, getChildrenNumber){
this.nameApp = "nameAppChanged";
this.gatto = "Miao";
}],
controllerAs: "ctrl"
}
},
resolve: {
getChildrenNumber: ["$http", function($http, httpPost){
var httpP = httpPost.get("http://domain.com/user/login/api-login", $.param({ username:"ciaociao6@ciao.com", password:"ciaociA0" }), httpConfig)
console.log(httpP);
return "Some response from an API";
}]
}
});
angular
.module("CiaoMiao")
.config(config);
As result of console.log I got "undefined", I don't get how to inject that factory at this part of the code. I tried to put into the main controller of that view, but it didn't work as well.
回答1:
(If you place the resolve at your view it should work. If you need that resolve in multiple views of the same state. I would try to use an abstract state and add the resolve there.) Works but is not needed (see edit below.)
Please have a look at this jsfiddle or the demo below.
I've reduced the demo a bit to keep things easier to read. Also not added array notation for DI in the demo.
You don't need to use the deferred object in your factory because $http
is already returning a promise.
Edit: You can place the resolve outside of the view and it should work too. I've tried it in this fiddle. So there's probably a different issue in your code.
OK, I think the problem with your code is that you're not returning the resolved value from your promise. Something like promise.then(function(response) {return response;});
angular.module('demoApp', ['ui.router'])
.factory('myDataFactory', function ($http) {
return {
get: function (id) {
return $http.post('http://crossorigin.me/http://www.mocky.io/v2/55a65562b2016ce10c7e6ea9', {
id: id
}).then(function (response) {
return response;
});
}
};
})
.config(function ($urlRouterProvider, $stateProvider) {
$urlRouterProvider.otherwise('/124');
$stateProvider.state('home', {
url: '/:id',
views: {
'viewA': {
template: '{{hello}}',
controller: 'homeController',
resolve: {
backendData: function (myDataFactory, $stateParams) {
return myDataFactory.get($stateParams.id);
}
}
},
'@': {
template: 'main view'
}
}
});
})
.controller('homeController', function ($scope, backendData) {
console.log(backendData);
$scope.hello = backendData;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js"></script>
<div ng-app="demoApp">
<div ui-view=""></div>
<div ui-view="viewA"></div>
</div>
回答2:
angular.module('demoApp', ['ui.router'])
.factory('AbcService', function ($resource) {
return $resource('app/rest/abc', {}, {
'query': { method: 'GET', isArray: true}
});
})
.config(function ($stateProvider) {
$stateProvider
.state('abc', {
templateUrl: "scripts/abc.html",
url: "^/abc",
controller: 'abcController',
resolve: {
resolvedData: ['AbcService', function(AbcService) {
return AbcService.query();
}]
}
})
})
.controller('abcController', function ($scope, resolvedData) {
$scope.data = resolvedData;
});
来源:https://stackoverflow.com/questions/31430231/how-can-i-use-a-factory-inside-a-resolve-of-stateprovider