M
M
microf2015-10-15 10:31:58
Angular
microf, 2015-10-15 10:31:58

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

Products service
(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;
        }
        }

})();

Controller
(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

2 answer(s)
A
amatory10, 2015-10-15
@amatory10

mine in the line: vm.products = ProductsPrepService.list(); - .list() would be redundant

_
_ _, 2015-10-15
@AMar4enko

Make it a rule in angularjs projects to have the following code:

.run(function($rootScope) {
   $rootScope.$on('$stateChangeError', function() {
        console.error(arguments[5]);
   });
});

Or use https://github.com/bendrucker/angular-router-excep...
Why are you using the forced $inject notation along with the @ngInject helper for ngAnnotate? If you currently have code minification enabled, then the problem is due to
resolve: {
                    ProductsPrepService: function(products) {

The function elements of the resolve section are also injected, they will not work without annotations in the minified version. And since this is a resolve section, angular will throw the exception into the scope, and not into the console.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question