Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 one way data binding on HTML like we do in angular 1 using ::

Is there any way in angular 2 so that we can achieve one-way data binding like we have done in angular 1.

<!DOCTYPE html>
<html lang="en-US">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="">
<h4>please change the text box value to see the scenario</h4>
  <p>Name : <input type="text" ng-model="name" 
ng-init="name='change me '"></p>
  <h1>Hello {{name}}</h1>
<h2>One-way binding- </h2>
{{::name}}
</div>

</body>
</html>

How we can achieve this in Angular 2 way. what alternatives we have for this.

Note: I do not want to reflect changes on HTML only. I'll need the updated value @component end, but I don't want to display the changes on UI only

like image 459
Ashish Ratan Avatar asked Apr 24 '26 12:04

Ashish Ratan


1 Answers

Two way data binding in Angular 2 works like below:

<input type="text"  [(ngModel)]="name">

One way data binding in Angular2 is achieved by removing paranthesis i.e [ngModel]

<input type="text"  [ngModel]="name" (ngModelChange)="onChange($event)">
onChange(newvalue) {
//work with newvalue
}

Hope it helps!

like image 132
Jayakrishnan Avatar answered Apr 27 '26 01:04

Jayakrishnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!