Answer the question
In order to leave comments, you need to log in
Why resolve doesn't work?
I register in resolve ui-router and nothing happens. No error in the console, nothing. During the transition (more precisely, when trying to transition) to the state, the products service should be called.
(function () {
'use strict';
angular
.module('products')
.config(configProducts);
configProducts.$inject = ['$stateProvider'];
/* @ngInject */
function configProducts($stateProvider) {
// Products state routing
$stateProvider
.state('products', {
url: '/products',
templateUrl: 'app/products/products.html',
controller : 'ProductsController',
controllerAs : 'products',
resolve: {
ProductsPrepService: function(products) {
console.log('resolve...')
return products.list();
}
}
});
}
})();
(function () {
'use strict';
angular
.module('products')
.factory('products', products);
products.$inject = [];
/* @ngInject */
function products() {
var service = {
list: list,
};
return service;
////////////////
function list() {
var data = [
{
'name': '1234567'
},
{
'name' : '987654321'
}]
return data;
}
}
})();
(function () {
'use strict';
angular
.module('products')
.controller('ProductsController', ProductsController);
ProductsController.$inject = ['ProductsPrepService'];
/* @ngInject */
function ProductsController(ProductsPrepService) {
var vm = this;
activate();
////////////////
function activate() {
vm.products = ProductsPrepService.list();
console.log(vm.products);
}
}
})();
Answer the question
In order to leave comments, you need to log in
mine in the line: vm.products = ProductsPrepService.list(); - .list() would be redundant
Make it a rule in angularjs projects to have the following code:
.run(function($rootScope) {
$rootScope.$on('$stateChangeError', function() {
console.error(arguments[5]);
});
});
resolve: {
ProductsPrepService: function(products) {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question