Skip to content

Instantly share code, notes, and snippets.

@johnpapa
Last active August 20, 2016 23:01
Show Gist options
  • Save johnpapa/9f6d8f89072077bcaff82bc03b9a31fe to your computer and use it in GitHub Desktop.
Save johnpapa/9f6d8f89072077bcaff82bc03b9a31fe to your computer and use it in GitHub Desktop.
Angular 2 data binding progression
<div>
<h2>Binding</h2>
<h4>Interpolation</h4>
<pre>Hello { {firstName }}</pre>
<div>Hello {{firstName}}</div>
<hr>
<h4>Property Binding Without Two-Way</h4>
<pre>&lt;input [value]="city"></pre>
<input [value]="city">
<p>City = {{city}}</p>
<hr>
<h4>Binding With Two-Way (Long Form)</h4>
<pre>&lt;input [value]="city" (input)="city = $event.target.value"></pre>
<input [value]="city" (input)="city = $event.target.value">
<p>City = {{city}}</p>
<hr>
<h4>Two-Way Binding (ngModel directive long form)</h4>
<pre>&lt;input [ngModel]="firstName" (ngModel)="firstName=$event"/> </pre>
<input [ngModel]="firstName" (ngModel)="firstName=$event"/> {{firstName}}
<hr>
<h4>Two-Way Binding (sugar)</h4>
<pre>&lt;input [(ngModel)]="lastName" /> </pre>
<input [(ngModel)]="lastName" /> {{lastName}}
<hr>
<h4>Two-Way Binding (classic style)</h4>
<pre>&lt;input bindOnNgModel="city" /></pre>
<input bindOnNgModel="city" /> {{city}}
<hr>
<h4>Local Variables</h4>
<pre>
&lt;input #mylocalvar/>{ {mylocalvar.value}}
&lt;button (click)="addThing(mylocalvar.value)">Add Local Var&lt;/button>
</pre>
<input #mylocalvar/>{{mylocalvar.value}}
<button (click)="addThing(mylocalvar.value)">Add Local Var</button>
<div>Async Piped Data from 500ms delay = {{ dataAsync | async | json }}</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment