I'm using ionic and I have the following view:
<ion-view hide-nav-bar="true" ng-controller="loginController" class="login-view">
<ion-content class="padding">
<div class="row row-center">
<div class="col">
<div id="logo"></div>
<form>
<div class="list">
<label class="item item-input">
<input type="text" placeholder="Membership No" ng-model="membershipNo">
</label>
<label class="item item-input">
<input type="password" placeholder="Password" ng-model="password">
</label>
</div>
<button class="button button-block button-positive button-login" ng-click="login()">
Login
</button>
</form>
</div>
</div>
</ion-content>
</ion-view>
And my controller:
app.controller('loginController', ['$scope', '$localstorage',
function($scope, $localstorage) {
$scope.membershipNo;
$scope.password;
$scope.login = function () {
console.log("User logged in with membership no: " + $scope.membershipNo +
"\n and password: " + $scope.password);
}
}
]);
What I don't understand, is that when I click the button, the login function is called correctly. Also, if in the controller I go and set $scope.membershipNo to something like "Banana Pancake", the view actually updates.
Yet when the login function actually runs, is says that membershipNo and password are undefined. I'm fairly new to Angular and Ionic so I know this is probably some n00b mistake...
membershipNois being passed by value, so what's being changed is not the reference in the controller. Create an Object, something like$scope.user = {}and pass thatng-model="user.membershipNo", otherwise Angular cannot send data back to the controller...)ngControllerand thengModelcreates a new scope. It's very bad practice. You really want "dots" in yourngModel, every time. Always. stackoverflow.com/a/17186640/624590 (It also won't work for Dean becauseionContentcreates a scope.)