I am trying to call angular routing inside a function in the controller, but it throws out "Uncaught TypeError: cannot read the path" of the property "undefined". I can not understand where I missed the injection in $ location, suppose this is the reason.
var gameApp = angular.module('gameApp', ['ngRoute']);
gameApp.config(function($routeProvider, $locationProvider, $provide) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/', {
templateUrl : 'home.html',
controller : 'mainController'
})
.when('/game', {
templateUrl : 'game.html',
controller : 'gameController'
})
.when('/game-over', {
templateUrl : 'game-over.html',
controller : 'mainController'
})
.otherwise({
redirectTo: '/'
});
});
And part of my game controller when I use a router
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');
}])
source
share