【发布时间】:2014-09-19 08:37:47
【问题描述】:
我试图在控制器中的函数内部触发 angularJS 路由,但它会返回“未捕获的类型错误:无法读取未定义的属性‘路径’”。看不到我错过了 $location 注入的地方,猜猜是这个原因。
var gameApp = angular.module('gameApp', ['ngRoute']);
gameApp.config(function($routeProvider, $locationProvider, $provide) {
$locationProvider.html5Mode(true);
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'home.html',
controller : 'mainController'
})
// route for the game page
.when('/game', {
templateUrl : 'game.html',
controller : 'gameController'
})
// route for the game over page
.when('/game-over', {
templateUrl : 'game-over.html',
controller : 'mainController'
})
// default
.otherwise({
redirectTo: '/'
});
});
当我使用路由器时,我的游戏控制器的一部分
gameApp.controller('gameController', ['$scope', '$location', function($scope, $location){
function gameLost($location){
var check = false;
console.log ('You lose! Your score is ')
$location.path('/game-over');
}])
【问题讨论】:
标签: javascript angularjs