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

[Angular2 Form] Create Radio Buttons for Angular 2 Forms

时间:2016-09-29 07:41:24      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels will match up with each input. This lesson shows how to use *ngFor with radio buttons and covers the quirks of the id property and forattributes as well as how to style validation of radio buttons.

 

  <!-- Radio button-->
  <form action="" name="myFom4" #myForm4="ngForm">
    <div *ngFor="let location of locations">
      <input type="radio"
             name="location"
             ngModel
             [value]="location"
             [id]="location"
             required
      >
      <label [attr.for]="location">{{location}}</label>
    </div>
  </form>
  <pre>
    {{myForm4.value | json}}
  </pre>

 

input.ng-invalid + label:after {
  content: ‘<--Requried to selet one‘
}

 

import {Component, OnInit} from ‘@angular/core‘;

@Component({
  selector: ‘app-message‘,
  templateUrl: ‘./message.component.html‘,
  styleUrls: [‘./message.component.css‘]
})
export class MessageComponent implements OnInit {

  locations: Array<string>;

  constructor() {
  }

  ngOnInit() {
    this.locations = [
      ‘China‘,
      ‘Finland‘,
      ‘Norway‘,
      ‘Japan‘
    ];
  }
}

 

Github

[Angular2 Form] Create Radio Buttons for Angular 2 Forms

标签:

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

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