码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular Form] ngModel and ngModelChange

时间:2017-01-20 19:35:12      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:oval   code   change   orm   now()   bool   sel   .com   json   

When using Radio button for Tamplate driven form, we want to change to the value change and preform some action.

 

import { Component, Input } from @angular/core;

import { Passenger } from ../../models/passenger.interface;

@Component({
  selector: passenger-form,
  styleUrls: [passenger-form.component.scss],
  template: `
    <form #form="ngForm" novalidate>
      {{ detail | json }}
      <div>
        Passenger name:
        <input
          type="text"
          name="fullname"
          [ngModel]="detail?.fullname">
      </div>
      <div>
        Passenger ID:
        <input
          type="number"
          name="id"
          [ngModel]="detail?.id">
      </div>
      <div>
        <label>
          <input 
            type="radio"
            [value]="true"
            name="checkedIn"
            [ngModel]="detail?.checkedIn"
            (ngModelChange)="toggleCheckIn($event)">
          Yes
        </label>
        <label>
          <input 
            type="radio"
            [value]="false"
            name="checkedIn"
            [ngModel]="detail?.checkedIn"
            (ngModelChange)="toggleCheckIn($event)">
          No
        </label>
      </div>
      <div *ngIf="form.value.checkedIn">
        Check in date:
        <input 
          type="number"
          name="checkInDate"
          [ngModel]="detail?.checkInDate">
      </div>
      {{ form.value | json }}
    </form>
  `
})
export class PassengerFormComponent {
  @Input()
  detail: Passenger;
  toggleCheckIn(checkedIn: boolean) {
    if (checkedIn) {
      this.detail.checkInDate = Date.now();
    }
  }
}

 

[Angular Form] ngModel and ngModelChange

标签:oval   code   change   orm   now()   bool   sel   .com   json   

原文地址:http://www.cnblogs.com/Answer1215/p/6323585.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!