2016-07-26 80 views
1

新属性“验证”我喜欢在我的angula2/ionic2应用以下形式:Angular2错误:无法在数

<form [ngFormModel]="subscribeForm" (ngSubmit)="onSubmit(subscribeForm.value)"> 
     ... 

     <ion-item> 
      <ion-label floating>Survey object size</ion-label> 
      <ion-input type="text" [ngFormControl]="size"></ion-input> 
     </ion-item> 

     <ion-item> 
      <ion-label floating>Survey object path</ion-label> 
      <ion-input type="text" [ngFormControl]="path" [(ngModel)]="path"></ion-input> 
     </ion-item> 

     <ion-item> 
      <ion-label floating>Survey object type</ion-label> 
      <ion-select [ngFormControl]="type"> 
       <ion-option *ngFor="let type of survey_types">{{type}}</ion-option> 
      </ion-select> 

     </ion-item> 


     <br/><br/> 
     <button type="submit" class="custom-button" block>Submit</button> 
    </form> 

而且我处理它在我的组件,如下所示:

export class Subscription{ 

    subscribeForm: ControlGroup; 

    object_name: any; 
    email: any; 
    subscriber_name: any; 
    size: any; 
    path: any; 
    cycle: AbstractControl; 
    cycle_options=['ONCE', 'WEEKLY', 'MONTHELY'] 

    type: AbstractControl; 
    survey_types=['FARM', 'SOLARPANEL', 'PLAIN'] 

    constructor(params: NavParams, private fb: FormBuilder) { 

     this.size = params.get('size'); 
     this.path = params.get('path'); 

     this.subscribeForm = fb.group({ 
      'object_name': '', 
      'email':'', 
      'subscriber_name':'', 
      'size':'', 
      'path':'', 
      'cycle':['', Validators.nullValidator], 
      'object_type':['', Validators.nullValidator] 
     }); 

     this.object_name = this.subscribeForm.controls['object_name'];  
     this.email = this.subscribeForm.controls['email']; 
     this.subscriber_name = this.subscribeForm.controls['subscriber_name']; 
     this.cycle = this.subscribeForm.controls['cycle']; 
     this.type = this.subscribeForm.controls['type']; 

    } 

    onSubmit(value){ 

     console.log(value) 
    } 
} 

有了这个,我得到一个错误:TypeError: Cannot create property 'validator' on number '14808.18276685957'

我的表单中有两个选择字段,我不确定我是否正确使用它们,并在我的组件中正确绑定它们的值。

我在做什么错?

更新:我得到两个NavParams并将它们的值分配给一个窗体控件,以便预先填充值。它看起来像它抛出一些验证错误Cannot create property 'validator' on number '14772.975244232435'这是路径

+0

如果您删除了其他所有内容,您是否仍然收到错误消息?如果不是,请删除与错误无关的代码。 –

+0

更新了我的HTML代码片段以及问题。 – Nitish

回答

0

更改ngFormControlngControl大小和路径领域解决了这个问题。

<ion-item> 
    <ion-label floating>Survey object size</ion-label> 
    <ion-input type="text" ngControl="size" [ngModel]="size" ></ion-input> 
</ion-item> 

<ion-item> 
    <ion-label floating>Survey object path</ion-label> 
    <ion-input type="text" ngControl="path" [ngModel]="path"></ion-input> 
</ion-item> 

但是,如果有人可以指出ngControl和ngFormControl之间有什么区别,那很好。

0

当你想使用模型的形式,你需要引用现场控制是这样的:

<ion-input type="text" 
    [ngFormControl]="subscribeForm.contrls.size"></ion-input> 

ngFormControl指令旨在引用现有的控制。

+0

ngControl和ngFormControl有什么区别?因为使用ngControl实际上解决了我的问题。 – Nitish

+0

此解决方案也给我一个错误:'TypeError:无法读取未定义的属性'大小' – Nitish

相关问题