angularJS 中的 $location 错误
$location error in angularJS
我有一个主控制器,如果用户名=admin 和密码=admin,我想设置登录页面将我带到“/tables”路径,但是我每次尝试登录时都会收到此错误
TypeError: Cannot read property 'path' of undefined
我在控制器和登录函数中添加了 $location
,但没有任何改变,一直出现同样的错误。如果我取出 $location
,我会得到这个错误
ReferenceError: $location is not defined
不确定在那里做什么。任何帮助表示赞赏。这是我的代码
angular
.module('RDash')
.controller('MasterCtrl', ['$scope', '$cookieStore', MasterCtrl]);
function MasterCtrl($scope, $cookieStore, $location) {
/**
* Sidebar Toggle & Cookie Control
*/
var mobileView = 992;
$scope.getWidth = function() {
return window.innerWidth;
};
$scope.login = function($location) {
if($scope.credentials.username !== "admin" && $scope.credentials.password !== "admin") {
alert("you are not the admin");
}
else{
$location.path('/tables');
}
};
}
login.html:
<div ng-controller="MasterCtrl">
<form >
<div class="jumbotron" class="loginForm">
<div class="form-group" id="loginPageInput">
<label for="exampleInputEmail1">User Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter Username" ng-model="credentials.username">
</div>
<div class="form-group" id="loginPageInput">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" ng-model="credentials.password">
</div>
<div id="loginPageInput">
<button ng-click="login()" type="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</form>
<div id="loginPageInput">
<div class="wrap">
<button ng-click="register()" class="btn btn-default" id="lefty" >Register</button>
<p ng-click="forgotpass()" id="clear"><a>Forgot my Password</a></p>
</div>
</div>
</div>
</div>
您错过了在 MasterCtrl
的依赖项数组中注入 $location
代码
angular
.module('RDash')
.controller('MasterCtrl', ['$scope', '$cookieStore', '$location', MasterCtrl]);
//^^^^^^^^
function MasterCtrl($scope, $cookieStore, $location) {
您还需要删除 $scope.login 参数 $location
,它正在终止服务 $location
从控制器注入的变量存在。
$scope.login = function ()
您没有将任何 $location
传递给 login
函数,因此它是 undefined
并且隐藏了外部 $location
局部变量。正确的代码应该是:
$scope.login = function () {
if ($scope.credentials.username !== "admin" && $scope.credentials.password !== "admin") {
alert("you are not the admin");
} else {
$location.path('/tables');
}
};
我有一个主控制器,如果用户名=admin 和密码=admin,我想设置登录页面将我带到“/tables”路径,但是我每次尝试登录时都会收到此错误
TypeError: Cannot read property 'path' of undefined
我在控制器和登录函数中添加了 $location
,但没有任何改变,一直出现同样的错误。如果我取出 $location
,我会得到这个错误
ReferenceError: $location is not defined
不确定在那里做什么。任何帮助表示赞赏。这是我的代码
angular
.module('RDash')
.controller('MasterCtrl', ['$scope', '$cookieStore', MasterCtrl]);
function MasterCtrl($scope, $cookieStore, $location) {
/**
* Sidebar Toggle & Cookie Control
*/
var mobileView = 992;
$scope.getWidth = function() {
return window.innerWidth;
};
$scope.login = function($location) {
if($scope.credentials.username !== "admin" && $scope.credentials.password !== "admin") {
alert("you are not the admin");
}
else{
$location.path('/tables');
}
};
}
login.html:
<div ng-controller="MasterCtrl">
<form >
<div class="jumbotron" class="loginForm">
<div class="form-group" id="loginPageInput">
<label for="exampleInputEmail1">User Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter Username" ng-model="credentials.username">
</div>
<div class="form-group" id="loginPageInput">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" ng-model="credentials.password">
</div>
<div id="loginPageInput">
<button ng-click="login()" type="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</form>
<div id="loginPageInput">
<div class="wrap">
<button ng-click="register()" class="btn btn-default" id="lefty" >Register</button>
<p ng-click="forgotpass()" id="clear"><a>Forgot my Password</a></p>
</div>
</div>
</div>
</div>
您错过了在 MasterCtrl
$location
代码
angular
.module('RDash')
.controller('MasterCtrl', ['$scope', '$cookieStore', '$location', MasterCtrl]);
//^^^^^^^^
function MasterCtrl($scope, $cookieStore, $location) {
您还需要删除 $scope.login 参数 $location
,它正在终止服务 $location
从控制器注入的变量存在。
$scope.login = function ()
您没有将任何 $location
传递给 login
函数,因此它是 undefined
并且隐藏了外部 $location
局部变量。正确的代码应该是:
$scope.login = function () {
if ($scope.credentials.username !== "admin" && $scope.credentials.password !== "admin") {
alert("you are not the admin");
} else {
$location.path('/tables');
}
};