#0006 How to use "ng-model" in AngularJS with EXAMPLES
Posted by Superadmin on November 10 2018 02:51:16

What is ng-model in AngularJs?

ng-model is a directive in Angular.JS that represents models and its primary purpose is to bind the "view" to the "model".

For example, suppose you wanted to present a simple page to the end user like the one shown below which asks the user to enter the "First name" and "Last name" in textboxes. And then you wanted to ensure that you store the information that the user has entered in your data model.

You can use the ng-model directive to map the text box fields of "First name" and "Last Name" to your data model.

The ng-model directive will ensure that the data in the "view" and that of your "model" are kept in sync the whole time.

ng-model in AngularJS – Learn in 10 Minutes!

In this tutorial, you will learn-

The ng-model Attribute

As discussed in the introduction to this chapter, the ng-model attribute is used to bind the data in your model to the view presented to the user.

The ng-model attribute is used for,

  1. Binding controls such as input, text area and selects in the view into the model.
  2. Provide a validation behavior - for example, a validation can be added to a text box that only numeric characters can be entered into the text box.
  3. The ng-model attribute maintains the state of the control (By state, we mean that the control and the data is bound to be always kept in sync. If the value of our data changes, it will automatically change the value in the control and vice versa)

How to use ng-model

1) Text Area

The text area tag is used to define a multi-line text input control. The text area can hold an unlimited number of characters, and the text renders in a fixed-width font.

So now let's look at a simple example of how we can add the ng-model directive to a text area control.

In this example, we want to show how we can pass a multiline string from the controller to the view and attach that value to the text area control.

ng-model in AngularJS – Learn in 10 Minutes!

<!DOCTYPE html>
<html>
<head>
	<meta chrset="UTF 8">
	<title>Event Registration</title>
	<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body >
<h1> Guru99 Global Event</h1>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>

<div ng-app="DemoApp" ng-controller="DemoCtrl">
	<form>
		&nbsp;&nbsp;&nbsp;Topic Description:<br> <br>
		&nbsp;&nbsp;&nbsp;
	<textarea rows="4" cols="50" ng-model="pDescription"></textarea><br><br> 
	</form>
</div>

<script>
	var app = angular.module('DemoApp',[]);
	app.controller('DemoCtrl', function($scope){
		$scope.pDescription="This topic looks at how Angular JS works \nModels in Angular JS"});
</script>

</body>
</html>

Code Explanation:

  1. The ng-model directive is used to attach the member variable called "pDescription" to the "textarea" control.

    The "pDescription" variable will actually contain the text, which will be passed on to the text area control. We have also mentioned 2 attributes for the textarea control which is rows=4 and cols=50. These attributes have been mentioned so that we can show multiple lines of text. By defining these attributes the textarea will now have 4 rows and 50 columns so that it can show multiple lines of text.

  2. Here we are attaching the member variable to the scope object called "pDescription" and putting a string value to the variable.
  3. Note that we are putting the /n literal in the string so that the text can be of multiple lines when it is displayed in the text area. The /n literal splits the text into multiple lines so that it can render in the textarea control as multiple lines of text.

If the code is executed successfully, the following Output will be shown when you run the code in the browser.

Output:

ng-model in AngularJS – Learn in 10 Minutes!

From the output

2) Input elements

The ng-model directive can also be applied to the input elements such as the text box, checkboxes, radio buttons, etc.

Let's look at an example of how we can use the ng-model with the "textbox" and "checkbox" input type.

Here we will have a text input type which will have the name of "Guru99" and there will be 2 checkboxes, one which will be marked by default and the other will not be marked.

ng-model in AngularJS – Learn in 10 Minutes!

<!DOCTYPE html>
<html>
<head>
	<meta chrset="UTF 8">
	<title>Event Registration</title>
	<link rel="stylesheet" href="css/bootstrap.css"/>
	<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
</head>
<body >
<h1> Guru99 Global Event</h1>

<div ng-app="DemoApp" ng-controller="DemoCtrl">
	<form>
		&nbsp;&nbsp;&nbsp;Topic Description:<br> <br>
		&nbsp;&nbsp;&nbsp;
        
		Name : <input type="text" ng-model="pname"><br>
		&nbsp;&nbsp;&nbsp;
		Topic : <br>&nbsp;&nbsp;&nbsp;
		<input type="checkbox" ng-model="Topic.Controller">Controller<br>&nbsp;&nbsp;&nbsp;
		<input type="checkbox" ng-model="Topic.Models">Models
	</form>
	</div>

<script>
	var app = angular.module('DemoApp',[]);
	app.controller('DemoCtrl', function($scope){
		$scope.pname="Guru99";
        
		$scope.Topic =
		{
			Controller:true,
			Models:false
		};     });
</script>

</body>
</html>

Code Explanation:

  1. The ng-model directive is used to attach the member variable called "pname" to the input type text control. The "pname" variable will contain the text of "Guru99" which will be passed on to the text input control. Note that any name can be given to the name of the member variable.
  2. Here we are defining our first checkbox "Controllers" which is attached to the member variable Topics.Controllers. The checkbox will be marked for this check control.
  3. Here we are defining our first checkbox "Models" which is attached to the member variable Topics.Models. The checkbox will not be marked for this check control.
  4. Here we are attaching the member variable called "pName" and putting a string value of "Guru99".
  5. We are declaring a member array variable called "Topics" and giving it two values, the first is "true" and the second is "false".

    So when the first checkbox gets the value of true, the check-box will be marked for this control, and likewise, since the second value is false, the check-box will not be marked for this control.

If the code is executed successfully, the following Output will be shown when you run your code in the browser.

Output:

ng-model in AngularJS – Learn in 10 Minutes!

From the output,

3) Select element form Dropdown

The ng-model directive can also be applied to the select element and be used to populate the list items in the select list.

Let's look at an example of how we can use the ng-model with the select input type.

Here we will have a text input type which will have the name of "Guru99" and there will be a select list with 2 list items of "Controller" and "Models".

ng-model in AngularJS – Learn in 10 Minutes!

<!DOCTYPE html>
<html>
<head>
	<meta chrset="UTF 8">
	<title>Event Registration</title>
	<link rel="stylesheet" href="css/bootstrap.css"/>
	<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
</head>
<body >
<h1> Guru99 Global Event</h1>

<div ng-app="DemoApp" ng-controller="DemoCtrl">
	<form>
		&nbsp;&nbsp;&nbsp;Topic Description:<br> <br>
		&nbsp;&nbsp;&nbsp;
        
		Name : <input type="text" ng-model="pName" value="Guru99"><br>
		&nbsp;&nbsp;&nbsp;
		Topic : <br>&nbsp;&nbsp;&nbsp;
        
		<select ng-model="Topics">
		<option>{{Topics.option1}}</option>
		<option>{{Topics.option2}}</option>
		</select>
	</form>
	</div>

<script>
	var app = angular.module('DemoApp',[]);
	app.controller('DemoCtrl', function($scope){
	$scope.pName="Guru99";
        
		$scope.Topics =
		{
			option1 : "Controller",
			option2 : "Module"
		};     });
</script>

</body>
</html>
  1. The ng-model directive is used to attach the member variable called "Topics" to the select type control. Inside of the select control, for each of the options, we are attaching the member variable of Topics.option1 for the first option and Topics.option2 for the second option.
  2. Here we are defining our Topics array variable which holds 2 key-value pairs. The first pair has a value of "Controllers" and the second has the value of "Models". These values will be passed to select input tag in the view.

    If the code is executed successfully, the following Output will be shown.

Output:

ng-model in AngularJS – Learn in 10 Minutes!

From the output, it can be seen that the value assigned to the pName variable is "Guru99" and we can see that the select input control has the options of "Controllers" and "Models".

Summary