$ state.go无法实例化模块

时间:2014-10-16 11:27:03

标签: javascript angularjs

我无法让$state.go('dashboard')工作。我收到错误:

Failed to instantiate module app.controllers.login due to:
Error: [$injector:nomod] http://errors.angularjs.org/1.2.19/$injector/nomod?p0=app...

当定义路线时,我没有错误。我做错了什么?

我也试过$stateProvider并得到同样的错误。

这是我的控制器......

App.js

    var app = angular.module('dashboardApp', [
    "ngRoute",
    "ngAnimate",
    "ngTouch",
    "mobile-angular-ui",
    "ui.router",
    "app.factories.storage",
    "app.controllers.main",
    "app.controllers.login",
    "angular-loading-bar"



]);

app.config(function ($stateProvider, $urlRouterProvider) {

    // For any unmatched url, send to /route1
    $urlRouterProvider.otherwise("/");

    $stateProvider
        .state('login', {
            url: "/",
            templateUrl: STATIC_URL + "html/company/login.html",
            controller: "loginController"


        })
        .state('dashboard', {
            url: "/dashboard",
            templateUrl: STATIC_URL + "html/company/dashboard.html"


        })


});

控制器:

angular.module('app.controllers.login', [
    "app.factories.http",
    "ui.router",

])
    .controller("loginController", ['$scope', "$location", "httpTokens", "httpFactory", "toaster", "$state",
        function ($scope, $location, httpTokens, httpFactory, toaster, $state ) {


            $scope.actionLoginCompanyUser = function () {
                var post_data = {username: $scope.user.email, password: $scope.user.password};

                httpTokens.createAccessTokens(post_data)
                    .then(function (responce) {

                        if (responce.status == 200) {
                            $state.go('dashboard')
                        }
                        else {
                            toaster.pop('error', "Incorrect Credentials", "Incorrect Email/Password");
                        }

                    })
            }


        }]);

1 个答案:

答案 0 :(得分:1)

您从未定义路线。

angular.module('app.controllers.login', ["app.factories.http","ui.router"])
     .config(function($stateProvider, $urlRouterProvider) {
        $stateProvider
                .state('dashboard', {
                    url: "/dashboard",
                    views: {
                        "view": {
                            templateUrl: "views/something.html"
                        }
                    }
                });

这应该有效!

如果您想要重新路由到登录页面,我认为,您还可以在配置中添加以下内容:

$urlRouterProvider.otherwise("login");

这样做的是,如果有人试图前往不存在的路线,它会将您送回登录。

注意:

如果您使用的是$q$timeout,请尝试使用$location.path('/dashboard');

有关该特定行为的更多信息:https://github.com/angular-ui/ui-router/issues/916