Angular.js:ReferenceError:范围未定义 [英] Angularjs: ReferenceError: scope is not defined

查看:112
本文介绍了Angular.js:ReferenceError:范围未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a beginner with Angularjs, and I have some difficulties understanding modules and scopes.

我不断收到范围未定义的错误,但我不明白为什么.首先,我将控制器链接到设置路线的位置,但是由于控制器内部的功能是在提交"按钮上单击时调用的,因此我将其删除了.我尝试过放回去,但这没什么区别.

I keep getting the error that the scope is undefined, but I don't get why. First I had my controller linked where I set my route, but since the function inside the controller is called on submit button click I took it away. I've tried putting it back, but that didn't make any difference.

这是我的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';
            });
    };
};

这是我的登录页面,尝试登录时出现错误:

And this is my login-page where I get the error, trying to log in:

<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>

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

I don't know if I should post the index-page as well, since the routing for home/about page works fine, so the problem is somewhere making the logincontroller and linking it to my submit button. I have found some similar questions, but I haven't seen anyone mix a new controller with myApp controllers, so I might be doing it completely wrong. I've also read that one html page can only have one module, so I didn't know if I could put the login controller apart. It would be nice to have some more info to be put in the right direction. Thanks in advance!

推荐答案

loginCtrl 中,您使用scope代替了$scope

问题解决了

  scope.$apply(function() 

使用

  $scope.$apply(function() 

这篇关于Angular.js:ReferenceError:范围未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆