I just need to store data in $scope.user on register() call, and trying to login using its data elements.. But not successful. I've used simplest way, I think, there may be other ways(like localStorage, cookies etc.) Im new to angular, don't know how to implement them. Please help me out...
Controller :
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!");
};
});
View :
<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>
Would really appreciate if someone could help me out with this problem.