我只需要在$scope.user
上调用register()
调用数据,并尝试使用其数据元素登录。但是不成功。我用最简单的方法,我认为,可能有其他方式(如localStorage,cookies等)。我是新来的角,不知道如何实现它们。请帮我...angular-js:无法访问同一控制器内不同函数调用中的数组元素
控制器:
var app = angular.module("app",[]);
app.controller("ctr",function($scope){
$scope.user = [{
fname:"",
lname: "",
username: "",
password: ""
}];
$scope.register = function(userData){
$scope.user.push(userData);
alert($scope.user.username); *//show undefined*
};
$scope.login = function(loginData){
if(loginData.username == $scope.user.username) {
alert("Login successfully!!");
}
else alert("Wrong user!");
};
});
查看:
<body ng-app="app">
<div ng-controller="ctr">
<table >
<tr>
<td><label>Username : </label></td>
<td><input type="text" ng-model="loginData.username" placeholder="Name" /></td>
</tr>
<tr>
<td><label>Password : </label></td>
<td><input type="password" ng-model="loginData.password" placeholder="Password" /></td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="Login" ng-click="login(loginData)" class="save"/>
</td>
</tr>
</table>
<table >
<tr>
<td><label>First Name : </label></td>
<td><input type="text" ng-model="userData.fname" placeholder="First Name" /></td>
</tr>
<tr>
<td><label>Last Name : </label></td>
<td><input type="text" ng-model="userData.lname" placeholder="Last Name" /></td>
</tr>
<tr>
<td><label>Username : </label></td>
<td><input type="text" ng-model="userData.username" placeholder="Name" /></td>
</tr>
<tr>
<td><label>Password : </label></td>
<td><input type="password" ng-model="userData.password" placeholder="Password" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="Register" ng-click="register(userData)" class="save"/></td>
</tr>
</table>
</div>
</body>
真的很感激,如果有人可以帮助我解决这个问题。