2017-09-18 10 views
0

양식 내에서 라디오 버튼 목록을 사용하는 데 문제가 있습니다. 내 코드 아래.
보기각도 - 라디오 버튼 목록이 작동하지 않습니다.

<form novalidate (ngSubmit)="onSubmit()" [formGroup]="perito"> 
    <div class="form-group row"> 
     <table class="table"> 
      <thead></thead> 
      <tbody> 
       <tr *ngFor="let p of periti"> 
        <td>{{p.nome}} {{p.cognome}}</td> 
        <td>{{p.indirizzo}} {{p.civico}} - {{p.cap}} 
         {{p.comune}} 
        </td> 
        <td><input formControlName="idPerito" type="radio" [value]="p.id" (change)="onSelect(p.id)"></td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
</form> 

컨트롤러

perito: FormGroup; 
ngOnInit() { 
    this.perito = new FormGroup({ 
     idPerito: new FormControl() 
    }); 
} 
onSelect() { 
    console.log(this.perito.value); 
} 

문제는 내가 한 라디오 버튼을 선택할 때이다 :

  1. 다른 모든 라디오 버튼
  2. 너무 선택하세요 형태 o 개체가 정의되지 않았습니다.

라디오 버튼 목록을 관리하는 올바른 방법은 무엇입니까? 감사. 그들은 기본 키 때문에

this.peritiSrv.getPeriti() 
    .then(res => { 
     this.periti = res; 
    }) 
    .catch(); 

어쨌든,이 두 periti 개체에 대한 동일한 ID를 가지고 일 할 수 없습니다 -

편집이 내 periti 배열을 채우는 방법이다.

+0

퍼티 배열을 제공 할 수 있습니까? 같은 어딘가에있을 수도 있습니다. 왜냐하면 라디오 버튼에 대한 값이 동일 할 때 이런 일이 발생할 수 있기 때문입니다. – lingthe

+0

내 편집 감사를 참조하십시오. – esseara

+0

나는 그 배열의 예를 보여 주려고했다. – lingthe

답변

0

아마도 구성 요소가 생성 될 때까지 인스턴스화되지 않았기 때문에 양식 객체는 undefined 일 것입니다 (construct ed). 문제가 지속되면

perito: FormGroup; 
constructor() { 
    this.perito = new FormGroup({ 
     idPerito: new FormControl('') 
    }); // empty string passed in as the initial value of the FormControl instead of no argument 
} 
// no ngOnInit() implementation 
onSelect() { 
    console.log(this.perito.value); 
} 

참조

컨트롤러의 ngOnInit() 후크에서 해당 구성 요소의 constructor의 본체에 formGroup 생성 라인을 이동하려고합니다.

+0

문제는 여전히 있습니다. 그곳에. 두 라디오 버튼을 선택했기 때문에 컨트롤러가 전달 된 값을 알지 못하기 때문에 객체가 정의되지 않았다고 생각합니다. – esseara