我无法让 $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
并得到相同的错误。
这是我的 Controller ...
应用程序.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"
})
});
Controller :
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");
}
})
}
}]);
最佳答案
您从未定义过路线。
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
关于javascript - $state.go 无法实例化模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26403233/