Error Module when inject 'ui.router'

岁酱吖の 提交于 2020-01-05 23:18:46

问题


When I use the ui.router, I got the error message "Error Module" and it failed to instantiate module xPlat. In html file I use the angular-ui-router verson 0.2.11

below is my injection of ui.route :

(function () {
    'use strict';
    angular.module('xPlat', ['ui.router'])
    .config(function ($stateProvider) {
        $stateProvider
            .state('views', {
                url: '/views',
                templateUrl: 'views/item.html'
            })
            .state('views.posItem', {
                url: '/views',
                templateUrl: 'views/posItem.html'
            })
            .otherwise('/views');
    })
})();

I don't understand about it, why it error?

this is error that I got :

Error: error:modulerr Module Error

Failed to instantiate module xPlat due to:
TypeError: undefined is not a function
    at http://localhost:4400/scripts/app.js:14:14
    at Object.d [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:30:210)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:29:58
    at Array.forEach (native)
    at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:7:261)
    at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:28:374)
    at Xb (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:32:427)
    at c (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:17:315)
    at Wb (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:18:30)
    at Oc (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:17:99

And description of error:

This error occurs when a module fails to load due to some exception. The error message above should provide additional context.


回答1:


.otherwise('/views'); is for the $urlRouterProvider not the $stateProvider

(function () {
'use strict';
angular.module('xPlat', ['ui.router'])
.config(function ($urlRouterProvider, $stateProvider) {
    $urlRouterProvider.otherwise('/views');
    $stateProvider
        .state('views', {
            url: '/views',
            templateUrl: 'views/item.html'
        })
        .state('views.posItem', {
            url: '/views',
            templateUrl: 'views/posItem.html'
        })            
})
})();



回答2:


You have to inject $stateProvider

(function () {
    'use strict';
    angular.module('xPlat', ['ui.router'])
    .config(['$stateProvider', function ($stateProvider) {
        $stateProvider
            .state('views', {
                url: '/views',
                templateUrl: 'views/item.html'
            })
            .state('views.posItem', {
                url: '/views',
                templateUrl: 'views/posItem.html'
            })
            .otherwise('/views');
    }])
})();



回答3:


I was getting same error until I changed the reference in my index.html from angular-ui-router.min.js to angular-ui-router.js

I know it may not be desired solution, but for me the minified library was just not working.

If you work with npm, your reference should look sth like this:

<script src="node_modules/angular-ui-router/release/angular-ui-router.js"></script>



回答4:


The way you load UI Router has changed. According to the docs (https://docs.angularjs.org/api/ngRoute), it needs to be done like this:

angular.module('app', ['ngRoute']);

This solved the problem for me.



来源:https://stackoverflow.com/questions/26491276/error-module-when-inject-ui-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!