Following are the key points described later in this article:
Lets look at how did Data Binding look like in AngularJS 1. It was termed as two-way data binding with two-way signifying following:
Following diagram depicts two way data binding in AngularJS 1.
Two way data binding in AngularJS 1
For example, in the sample code below, there is an input element with ng-model assigned to “name”. And, then, the “name” is accessed/displayed elsewhere in the view using the syntax such as {{name}}. See the code such as <h1>Hello, {{name}}</h1>. With two-way data binding, any change in the input field will update the view with changed value.
<body ng-app="HelloApp" ng-controller="HelloCtrl">
<h1>Hello, {{name}}</h1>
<hr/>
<form>
<div>
<input type="text" ng-model="name">
</div>
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.min.js"></script>
<script type="text/javascript">
angular.module('HelloApp', [])
.controller('HelloCtrl', ['$scope', function($scope){
$scope.name = "Calvin";
}])
</script>
</body>
In Angular 2, following diagram depicts the two-way data binding:
Data Binding in Angular 2
Take a look at following code:
@Component({
selector: 'hello'
directives: [UserComponent]
})
@View({
template = `
<h1>Hello, {{name}}</h1>
<form>
<div>
<input type="text" [(ng-model)]="name" (change)="updateCount()" username={{name}}/>
</div>
</form>
`
})
export class HelloComponent {
name = 'Calvin Hobbes';
count = 1;
updateCount = function() {
this.count++;
};
}
Following is how the code for “UserComponent” would look like. Note the usage of “inputs: [‘username’] for making the username as input property.
@Component({
selector: 'user',
inputs: ['username']
})
@View({
template: `
My name is {{username}}!
`
})
export class UserComponent {
username: '';
}
In the diagram and code shown above, note the following:
The above expressions is used to update the data from component (data source) to the HTML view. With interpolation, the property is put into the template with double curly braces such as {{name}} in the code shown below.
Two way data binding
In the same way, with parent-child relationships, the changes made in child component’s template flows to parent component as in event binding, and changes made in property of parent component flows to the child component as in property binding. Following diagram depicts the same:
Two way Data Binding vis-a-vis Parent Child Component Relationship
Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…
Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…
Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…
Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…
The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…
Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…