나 m 을 사용하여 각도에서 확인란을 선택 하나 이상의 데이터 및 im 을 얻으려고의 가치는 확인란에 양식을 제출합니다.대신 값을 가져 im 치지 않으면 안 된다는 것입니다.가 다음 코드는 데 도움이 나를 사전에 감사합니다
export class CreatesessionComponent implements OnInit {
form : FormGroup ;
constructor(private formBuilder: FormBuilder) {
}
ngOnInit() {
this.form = this.formBuilder.group({
useremail : new FormArray([
new FormControl('',Validators.required)
])
});
}
}
userdata 는 동적 배열하는 것을 얻는 데이터베이스에서
<div class = "row" formArrayName="useremail; let k = index">
<div class="col-md-8 col-sm-5 col-xs-12 col-lg-8">
<div *ngFor="let data of userdata">
<div class="col-md-6">
<input type="checkbox" name="useremail" formControlName ="{{k}}" [value]="data.email">{{data.email}}
</div>
</div>
</div>
</div>
이후 여러 값을 사용하려면,당신은 당신을 사용할 필요가FormArray
,이후FormControl
을 캡처 할 수 있습니다.
과 함께 시작을 선언하는 빈 formArray:
this.myForm = this.fb.group({
useremail: this.fb.array([])
});
반복하여 귀하의 이메일,그리고 시계에 대한 변경 이벤트를 통과하는 각각의 이메일 및 이벤트하는 방법을항상
이 있는지 확인 하's확인
다음에 추가 해당 이메일을 formarray 는 경우,그's 되지 않은 제거하는 선택에서 전자 메일 양식을 배열:
<div *ngFor="let data of emails">
<input type="checkbox" (change)="onChange(data.email, $event.target.checked)"> {{data.email}}<br>
</div>
그리고onChange
:
onChange(email:string, isChecked: boolean) {
const emailFormArray = <FormArray>this.myForm.controls.useremail;
if(isChecked) {
emailFormArray.push(new FormControl(email));
} else {
let index = emailFormArray.controls.findIndex(x => x.value == email)
emailFormArray.removeAt(index);
}
}
문제@AJT_82 대답하기를 원하는 경우 수정을 사용하여 모델형태입니다.reset()거나 또는 양식입니다.patchValue()*그't work. 이러한 문제를 해결하를 구현하는 데 필요ControlValueAccessor인터페이스입니다. 기본적으로 만들어야 하는 2 개의 구성 요소 그룹의 구성 요소를 보유하는 모델은 가치 구현 ControlValueAccessor 및 checkbox 실제적인 확인란을 선택합니다. 내가 쓴블로그 포스팅자세한 설명과뿐만 아니라 만들어plunker명하는 몇 가지 예입니다.
최종 사용:
<checkbox-group [(ngModel)]="selectedItems">
<checkbox value="item1">Item 1</checkbox>
<checkbox value="item2">Item 2</checkbox>
<checkbox value="item3">Item 3</checkbox>
<checkbox value="item4">Item 4</checkbox>
</checkbox-group>
구현의 그룹의 구성 요소:
@Component({
selector: 'checkbox-group',
template: `<ng-content></ng-content>`,
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CheckboxGroupComponent),
multi: true
}]
})
export class CheckboxGroupComponent implements ControlValueAccessor {
private _model: any;
// here goes implementation of ControlValueAccessor
...
addOrRemove(value: any) {
if (this.contains(value)) {
this.remove(value);
} else {
this.add(value);
}
}
contains(value: any): boolean {
if (this._model instanceof Array) {
return this._model.indexOf(value) > -1;
} else if (!!this._model) {
return this._model === value;
}
return false;
}
// implementation for add and remove
...
}
Checkbox:
@Component({
selector: 'checkbox',
template: `
<div (click)="toggleCheck()">
<input type="checkbox" [checked]="isChecked()" />
<ng-content></ng-content>
</div>`
})
export class CheckboxComponent {
@Input() value: any;
constructor(@Host() private checkboxGroup: CheckboxGroupComponent) {
}
toggleCheck() {
this.checkboxGroup.addOrRemove(this.value);
}
isChecked() {
return this.checkboxGroup.contains(this.value);
}
}
아동 확인란트가 참조하여 그룹의 구성 요소(@Host()장식). 경우 체크 박스를 클릭*toggleCheck()*전화 addOrRemove()방법에 그룹 구성 요소는 경우 체크 박스's 값은 이미 모델에서,그것은 제거,그렇지 않으면 그것은 모델에 추가됩니다.
았다에서 초기에 반드시 한번 함수를 입력 로드 형태로 자동 선택:
[확인]="this.선택해야 하는가? 다.선택됩니다.유형입니다.을 포함한(type):false"
전체 코드:
<label *ngFor="let type of this.entityType" class="checkbox-inline c-checkbox">
<input type="checkbox" [checked]="this.selected? this.selected.type.includes(type) : false" (change)="onChangeEntityType(type, $event.target.checked)"/>
<span class="fa fa-check"></span>{{type | translate}}</label>
가
this.선택
내체고
this.선택됩니다.유형입니다.을 포함한(type)
자동 체크하는 경우 체크 박스를 검사할 것입니다.
나는 해결에 두 개의 서로 다른 방법 과 하나란 간단한 어레이 매트-체크 박스 과 다른 매트 선택-목록입니다.
의 요점 코드에 게시합니다. 에 값을 설정할 수 있습니다 모든 종류의 조합. 내 경우에는've 사용 concatination 의 id 및 설명하기 위해서 init 양식 컨트롤 끝에 하나의 소스에서..
내 문제가 있었으로 청소 체크 박스 배열에서 더 간단한 예.. 시간 낭비하지 않으려면..따라 실행하고 다음 작업을..:)