Javascript 在所有尝试之后,ng模型变量在控制器中保持未定义状态
大家好! 我最近开始使用离子与angular,并试图编写一个登录表单。使用了一些教程,但我不能让它工作 首先,我通过stackoverflow搜索这个问题。其他主题包含建议,如在提及ng模型时使用圆点。或者所描述的问题与我的有点不同,即使用电子邮件类型作为输入字段。 我在login.html中对ng模型的引用中有一个点(user.username,user.password): 在controllers.js中,我有Javascript 在所有尝试之后,ng模型变量在控制器中保持未定义状态,javascript,angularjs,ionic-framework,Javascript,Angularjs,Ionic Framework,大家好! 我最近开始使用离子与angular,并试图编写一个登录表单。使用了一些教程,但我不能让它工作 首先,我通过stackoverflow搜索这个问题。其他主题包含建议,如在提及ng模型时使用圆点。或者所描述的问题与我的有点不同,即使用电子邮件类型作为输入字段。 我在login.html中对ng模型的引用中有一个点(user.username,user.password): 在controllers.js中,我有 quest.controller('LoginCtrl', function(
quest.controller('LoginCtrl', function($scope, $rootScope, LoginService, $ionicPlatform, $ionicPopup, $state){
$scope.user = {};
$scope.login = function(){
alert($scope.user.username + "\n" +
$scope.user.password);
LoginService.loginUser($scope.user.username,
$scope.user.password).success(function(user) {
alert("admin logged on");
}).error(function(user) {
var alertPopup = $ionicPopup.alert({
title: 'Inrorrect login/password!',
template: 'Invalid credentials!'
});
});
}
console.log("Logged on as '" + $scope.user.username +
"', using pswd '" + $scope.user.password);
});
我还使用该服务检查输入的凭据,但我不认为这是错的
控制器在单击登录窗体中的按钮后运行。紧接着,我收到一个带有未定义变量的警报,它们也会以未定义的形式记录。
我真的被卡住了,有点沮丧)。请帮帮忙!
也许我应该每隔一个文件重新初始化我的模块
提前感谢。因为您仅将控制器绑定到按钮元素:
<button class="button button-block button-calm"
ng-click="login()" ng-controller="LoginCtrl">Login</button>
登录
您必须将控制器与包含所有代码的元素绑定在一起,您希望这些代码成为控制器作用域的一部分,如下所示:
<ion-view view-title="Enter username" name="login-view">
<ion-content class="padding">
<div ng-controller="LoginCtrl"> <!-- Here -->
<div class="list list-inset">
<label class="item item-input">
<input type="text" placeholder="username"
ng-model="user.username" >
</label>
<label class="item item-input">
<input type="password" placeholder="password"
ng-model="user.password">
</label>
</div>
<button class="button button-block button-calm"
ng-click="login()">Login</button>
</div>
</ion-content>
登录
因为您仅将控制器绑定到按钮元素:
<button class="button button-block button-calm"
ng-click="login()" ng-controller="LoginCtrl">Login</button>
登录
您必须将控制器与包含所有代码的元素绑定在一起,您希望这些代码成为控制器作用域的一部分,如下所示:
<ion-view view-title="Enter username" name="login-view">
<ion-content class="padding">
<div ng-controller="LoginCtrl"> <!-- Here -->
<div class="list list-inset">
<label class="item item-input">
<input type="text" placeholder="username"
ng-model="user.username" >
</label>
<label class="item item-input">
<input type="password" placeholder="password"
ng-model="user.password">
</label>
</div>
<button class="button button-block button-calm"
ng-click="login()">Login</button>
</div>
</ion-content>
登录
是否正确设置了ngApp
和ngController
?console
中的任何错误是否正确设置了ngApp
和ngController
?控制台中有错误吗?