Dean Gibson Dean Gibson - 1 year ago 77
AngularJS Question

ng-model is undefined in controller

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>

<div class="list">
<label class="item item-input">
<input type="text" placeholder="Membership No" ng-model="membershipNo">
<label class="item item-input">
<input type="password" placeholder="Password" ng-model="password">

<button class="button button-block button-positive button-login" ng-click="login()">



And my controller:

app.controller('loginController', ['$scope', '$localstorage',
function($scope, $localstorage) {


$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
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...

Answer Source

I had exact the same issue recently and that's probably a solution:

Modified quote:

"If you use ng-model, you have to have a dot in there."
Make your model point to an and you'll be good to go.


$scope.formData = {};
$scope.login = function () {
  console.log("User logged in with membership no: " + $scope.formData.membershipNo +
  "\n and password: " + $scope.formData.password);


<input type="text" placeholder="Membership No" ng-model="formData.membershipNo">
<input type="password" placeholder="Password" ng-model="formData.password">