Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ng-model undefined in the controller

I am having some "problems" reading an ng-model from the controller.

I want to do this:

<input type="text" ng-model="code">
<button ng-click="setCode()">Login</button>

And in my controller access that variable like this:

 $scope.setCode = function(){
    alert($scope.code);
  }

That is what I want to do but my code variable is undefined.

I found 2 ways to get this to work:

1) Passing the variable as an argument

<input type="text" ng-model="code">
<button ng-click="setCode(code)">Login</button>

and:

$scope.setCode = function(code){
    alert(code);
  }

2) Declaring my variable as an object

<input type="text" ng-model="code.text">
<button ng-click="setCode()">Login</button>

and:

$scope.code = {text: 'foo'};

[...]

$scope.setCode = function(){
    console.log($scope.code);
  }

(I prefer the second one)

But I am a little confused about what I should do. It's ok to declare my ng-models like objects? I have to declare ALL my models like objects?


EDIT: Here is a Plnkr of the problem

In this example (I am using the Ionic framework), I declare a variable code with the value test, when I change the model in the input and press the Start button, in theory I have to see in an alert the new value of the model. But what I see is the old one.

Thanks!

like image 810
Mati Tucci Avatar asked Mar 31 '14 13:03

Mati Tucci


People also ask

How is NG-model defined?

The ngModel directive is a directive that is used to bind the values of the HTML controls (input, select, and textarea) or any custom form controls, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during form validations.

What is Ng-controller in HTML?

The ng-controller directive adds a controller to your application. In the controller you can write code, and make functions and variables, which will be parts of an object, available inside the current HTML element. In AngularJS this object is called a scope.

How does an NG-controller work?

The ng-controller directive is used to specify a controller in HTML element, which will add behavior or maintain the data in that HTML element and its child elements. The following example demonstrates attaching properties to the $scope object inside a controller and then displaying property value in HTML.


2 Answers

I fixed your plnkr example using object model instead of primitive type.

$scope.model = {};
$scope.model.code = "test";

$scope.setCode = function(){
    alert($scope.model.code);
}

See the plnkr.

like image 148
xi.lin Avatar answered Oct 17 '22 11:10

xi.lin


Your first example is working, here is a plunkr.

If you hit the button without entering any text into the input field you get an alert undefined. $scope.code gets defined the moment you enter something to its bound field ng-model="code".

You could initialize it to a default in your controller too:

$scope.code = 'my default value';
like image 41
nilsK Avatar answered Oct 17 '22 12:10

nilsK