Javascript angularJS中的$location错误

Javascript angularJS中的$location错误,javascript,angularjs,dependency-injection,url-routing,Javascript,Angularjs,Dependency Injection,Url Routing,我有一个主控制器,如果username=admin和password=admin,我想设置登录页面,将我带到“/tables”路径,但是每次尝试登录时都会出现此错误 TypeError: Cannot read property 'path' of undefined 我在控制器和登录功能中添加了$location,但没有任何更改,仍然会出现相同的错误。如果我取出$location,则会出现此错误 ReferenceError: $location is not defined 不知道在那里

我有一个主控制器,如果username=admin和password=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

代码

angular
    .module('RDash')
    .controller('MasterCtrl', ['$scope',  '$cookieStore', '$location', MasterCtrl]);
                                                          //^^^^^^^^
function MasterCtrl($scope, $cookieStore, $location) {
您还需要删除$scope.login参数
$location
,该参数将终止从控制器注入的服务
$location
变量存在


$scope.login=function()

MasterCtrl

代码

angular
    .module('RDash')
    .controller('MasterCtrl', ['$scope',  '$cookieStore', '$location', MasterCtrl]);
                                                          //^^^^^^^^
function MasterCtrl($scope, $cookieStore, $location) {
您还需要删除$scope.login参数
$location
,该参数将终止从控制器注入的服务
$location
变量存在


$scope.login=function()

您没有将任何
$location
传递给
login
函数,因此它是
未定义的
$location
局部变量。正确的代码应为:

$scope.login = function () {
    if ($scope.credentials.username !== "admin" && $scope.credentials.password !== "admin") {
        alert("you are not the admin");
    } else {
        $location.path('/tables');
    }
};

您没有将任何
$location
传递给
login
函数,因此它是
未定义的
$location
局部变量。正确的代码应为:

$scope.login = function () {
    if ($scope.credentials.username !== "admin" && $scope.credentials.password !== "admin") {
        alert("you are not the admin");
    } else {
        $location.path('/tables');
    }
};

不要编辑你的问题代码以匹配人们在答案中建议的改进。。现在,每个第一次看到这个问题的人都会想:“达夫,ppl建议的所有东西都在他的代码中准备好了…”Guinn我会收回它,谢谢你让我知道不要编辑你的问题代码来匹配人们在答案中建议的改进。。现在,每个第一次看到这个问题的人都会想:“达夫,ppl建议的所有东西都在他的代码中准备好了……”Guinn我会收回的,谢谢你让我知道