2017-03-03 115 views
0

我正在構建一個angular2應用程序,其中,我在表單驗證中使用了md-slide-toggle。在onSubmit期間,當我控制該值時,默認情況下它會返回空值。如何分配MD-撥動滑動默認是假angular2材料設計問題

<form #formtest=ngForm (ngSubmit)=onSubmit(formtest.value)> 

    <div class="form-group" *ngFor="let temp of value"> 
    <md-slide-toggle [checked]="isChecked" color="primary" name={{temp}} ngModel> {{temp}}</md-slide-toggle> 
    </div> 
    <button md-raised-button type="submit">Check</button> 
    </form> 

是否有這個問題

回答

1

在你app.component.ts,從@angular/core進口OnInit,然後你必須初始化負載的東西添加ngOnInit()

app.component.ts

import {Component, OnInit} from '@angular/core'; 
// Your other imports here 

@Component({ 
    selector: 'app-root', 
    templateUrl: '/path/to/app.component.html' 
}) 

export class AppComponent implements OnInit { 
    checked: boolean; 
// NgOnInit is required or else Angular will throw an error 
    ngOnInit() { 
     this.checked = false; 
    } 
} 

另外,這樣做:

import {Component} from '@angular/core'; 
// Your other imports here 

@Component({ 
    selector: 'app-root', 
    templateUrl: '/path/to/app.component.html' 
}) 
export class AppComponent { 
    checked: boolean = false; 
} 
+0

即使這種方法的作品 – skid

0

的問題可以通過以下簡單的方法創建在組件文件中的變量來解決任何人。

public value =[{name:'one' ,checked:false}, 
    {name:'two' ,checked:false}, 
    {name:'three' ,checked:false}]; 

將它們綁定到標籤,如下HTML

<form #formtest=ngForm (ngSubmit)=onSubmit(formtest.value)> 

    <div class="form-group" *ngFor="let temp of value"> 
    <md-slide-toggle color="primary" name={{temp.name}} [(ngModel)] = "temp.checked" > {{temp.name}}</md-slide-toggle> 
    </div> 
    <button md-raised-button type="submit">Check</button> 
    </form>