2017-03-03 113 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>