The properties of the permission object are not passed to the controller; unknown provider error

Before voting, yes, this question has already been asked and answered, but the answers are not satisfactory. They all correctly suggest adding a controller to the route configuration, but this is not the case here.

The expected behavior of the routeProvider object resolve xxxmust be entered into the controller:

var app = angular.module('X', [])
.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/',{
        controller:'XCtrl',
        templateUrl: 'x.html',
        resolve: {
            xxx: function () {
                return 'XXX from routing config.';
            }
        }
    })
}])
.controller('XCtrl', function($scope, xxx) {
    console.log('xxx = '+xxx);
});

The console should get a record xxx = XXX from routing config..

Instead, fetching the code above fails:

Error: [$injector:unpr] Unknown provider: xxxProvider <- xxx
http://errors.angularjs.org/1.2.10/$injector/unpr?p0=xxxProvider%20%3C-%20xxx
 .. etc.

XCtrlIt is not declared in HTML with a directive ng-controller, but is defined only in the routing configuration.

+4
source share
1 answer

, , , resolve:

var app = angular.module('X', [])
.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
    .when('/',{
        controller:'XCtrl',
        templateUrl: 'x.html',
        resolve: {
            xxx: function () {
                return 'XXX from routing config.';
            }
            another: // ...
        }
    })
    .when('/page2',{
        controller:'XCtrl',
        templateUrl: 'x/p2.html',
        resolve: {
            xxx: function () {
                return 'XXX from routing config.';
            }
        }
    })
}])
.controller('XCtrl', function($scope, xxx) {
    console.log('xxx = '+xxx);
});

resolve promises, , , . Promises , .

+3

Source: https://habr.com/ru/post/1526611/


All Articles