ng-model directive

Ng-model is used to bind input fields, select and textarea with model and we can use this model in page. Ng-model provides the facility of two-way data binding.

Example of ng-model

We have following textbox. And we want as user will type the data it will show in paragraph tag. If we have to do this task without angularjs then we have to use javascript and need to use the event on which it will display textbox text in paragraph. But with the help of ng-model we will bind this input control with model and after that we can use this model anywhere.

		
<!DOCTYPE html>
<html>
<head>
    <title>AngularJS ng-model example</title>
    <meta charset="UTF-8">
    
       <script src="angular.min.js"></script>
</head>
<body ng-app>
<input type="text" ng-model="t1">
    <p>{{ t1 }}</p>

</body>
</html>

		
		

Now execute this code and you will get following output

		
		what is ng-model
		Figure 1
		
		

ng-bind

In the above example we have used binding expression to bind the model data. We can also use ng-bind to show model data in application.

ng-bind example

		
		<!DOCTYPE html>
<html>
<head>
    <title>AngularJS ng-model example</title>
    <meta charset="UTF-8">
    
       <script src="angular.min.js"></script>
</head>
<body ng-app>
<input type="text" ng-model="t1">

    <div ng-bind="t1"></div>

</body>
</html>

		
		

Now execute this code and you will get same output