✔ AngularJS Data Binding
> Data Binding in AngularJS is the synchronization between the model and the view
1. Data Model
> AngularJS applications usually have a data model(collection of data available for the application)
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
2. HTML View
> the HTML container where the AngularJS application is displayed, is called view
> view has access to the model, and there are several ways of displaying model data in the view
> can use the 'ng-bind' directive, which will bind the innerHTML of the element to the specified model property
<p ng-bind="firstname"></p>
<p>First name: {{firstname}}</p>
or you can use the 'ng-model' directive on HTML controls to bind the model to the view
3. The ng-model Directive
> use the 'ng-model' directive to bind data from the model to the view on HTML controls (input, select, textarea)
<input ng-model="firstname">
4. Two-way Binding
> Data binding in AngularJS is the synchronization between the model and the view
> when data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well. This happens immdiately and automatically, which makes sure that the model and the view is updated at all times.
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="firstname">
<h1>{{firstname}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
</script>
5. AngularJS Controller
> Because of the immediate synchronization of the model and the view, the controller can be completely separated from the view, and simply concentrate on the model data. The view will reflect any changes made in the controller.
<div ng-app="myApp" ng-controller="myCtrl">
<h1 ng-click="changeName()">{{firstname}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.changeNmae = function() {
$scope.firstname = "Nelly";
}
});
</script>
'IT' 카테고리의 다른 글
AngularJS #9 - Scope (0) | 2021.12.22 |
---|---|
AngularJS #8 - Controllers (0) | 2021.12.21 |
AngularJS #6 - ng-model Directive (0) | 2021.12.19 |
AngularJS #5 - Directives (0) | 2021.12.18 |
AngularJS #4 - Modules (0) | 2021.12.18 |