【问题标题】:Angularjs: ReferenceError: scope is not definedAngularjs:ReferenceError:未定义范围
【发布时间】:2014-01-10 17:14:02
【问题描述】:

我是 Angularjs 的初学者,在理解模块和作用域方面有些困难。

我不断收到范围未定义的错误,但我不明白为什么。首先,我将控制器链接到我设置路由的位置,但是由于在单击提交按钮时调用了控制器内部的函数,因此我将其拿走了。我试过把它放回去,但这没有任何区别。

这是我的 js 文件:

var myApp = angular.module('myApp', ['ngRoute']);

// routes
myApp.config(function($routeProvider) {
    $routeProvider

        // route for the home page
        .when('/', {
            templateUrl : 'home.html',
            controller  : 'mainController'
        })

        // route for the about page
        .when('/about', {
            templateUrl : 'about.html',
            controller  : 'aboutController'
        })

        // route for the login page
        .when('/login', {
            templateUrl : 'login.html'
        });
});


myApp.controller('mainController', function($scope) {
    $scope.message = 'Beginpagina';
});

myApp.controller('aboutController', function($scope) {
    $scope.message = 'Informatie over deze pagina';
});


function loginCtrl($scope, $http){
    $scope.doLogin = function() {

    $http({

                method: 'POST', 
                url: 'loginController.php',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                data: { 
                        'username': $scope.un, 
                        'password': $scope.pw 
                     },
            }).

            success(function(data, status) {

                $scope.data = data;
                if(data == 'FALSE'){
                    $scope.errorMessage = 'Geen geldige inloggegevens';
                } else {
                   scope.$apply(function() { $location.path("/profile"); });
                }

            }).

            error(function(data, status) {
                $scope.data = data || "FALSE";
                $scope.errorMessage = 'Something went wrong';
            });
    };
};

这是我收到错误的登录页面,正在尝试登录:

<div class="span5" ng-controller="loginCtrl"> 
    <form>
       <fieldset>
              <div class="form-group">
                <input class="form-control" placeholder="Gebruikersnaam" ng-model="un" 
                  type="text" autocomplete="off">
               </div>
               <div class="form-group">
                  <input class="form-control" placeholder="Password" ng-model="pw"  
                    type="password" value="" autocomplete="off">
               </div>
               <input class="btn btn-lg btn-success btn-block" type="submit" 
                 ng-click="doLogin()" value="Login">
               <div>{{errorMessage}}</div>
           </fieldset>
       </form>
   </div>

我不知道我是否也应该发布索引页面,因为主页/关于页面的路由工作正常,所以问题出在某个地方制作登录控制器并将其链接到我的提交按钮。我发现了一些类似的问题,但我没有看到有人将新控制器与 myApp 控制器混合使用,所以我可能做错了。我也读过一个html页面只能有一个模块,所以我不知道我是否可以将登录控制器分开。如果有更多信息可以放在正确的方向上,那就太好了。提前致谢!

【问题讨论】:

  • 正如@Satpal 指出的错误来自您引用scope 而不是scope 的事实。但是,您也不需要$scope.$apply()。承诺的 success 回调已经在 $digest 中。
  • 好吧.. 有点尴尬:) 只是证明我需要休息一下.. 在成功登录后我将如何路由到一个页面,因为 $location 似乎未定义为好吧:/

标签: angularjs


【解决方案1】:

loginCtrl中,您使用的是scope而不是$scope

有问题

  scope.$apply(function() 

使用

  $scope.$apply(function() 

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2011-08-30
    • 2015-08-20
    • 2012-05-11
    相关资源
    最近更新 更多