我是
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>
我不知道是否应该发布索引页面,因为home / about页面的路由工作正常,所以问题是在哪里制作logincontroller并将其链接到我的提交按钮.我发现了一些类似的问题,但我没有看到有人将新控制器与myApp控制器混合在一起,所以我可能完全错了.我还读到一个html页面只能有一个模块,所以我不知道是否可以将登录控制器分开.将更多信息放在正确的方向上会很不错.提前致谢!
解决方法
在loginCtrl中,您使用了scope而不是$scope
问题是符合的
scope.$apply(function()
使用
$scope.$apply(function()