Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Ionic2:输入字段,为什么我得到值中的空字符串_Angularjs_Angular_Ionic2_Ionic View_Angular2 Forms - Fatal编程技术网

Angularjs Ionic2:输入字段,为什么我得到值中的空字符串

Angularjs Ionic2:输入字段,为什么我得到值中的空字符串,angularjs,angular,ionic2,ionic-view,angular2-forms,Angularjs,Angular,Ionic2,Ionic View,Angular2 Forms,这是我的客户端: <ion-card *ngFor="#p of posts | async"> <ion-card-header> {{p.title}} </ion-card-header> <ion-card-content> <form [ngFormModel] = 'form' (ngSubmit) = 'addcomment(form.value, p.$key)'>

这是我的客户端:

<ion-card *ngFor="#p of posts | async">
    <ion-card-header>
        {{p.title}}
    </ion-card-header>

    <ion-card-content>
        <form [ngFormModel] = 'form' (ngSubmit) = 'addcomment(form.value, p.$key)'>
            <ion-input  type="text" placeholder="your comment" (ngModel) = 'comment'></ion-input>
            <button>add comment</button>
        </form>
    </ion-card-content>
</ion-card>
如果我在控制台中打印
表单.value
内部
addcomment()


this.comment
AbstractControl
类内变量的类型)为空。

这仅从输入字段更新
comment

(ngModel) = 'comment'>
但是如果
comment
发生更改,则不会更新输入字段

改用双向绑定

    [(ngModel)] = 'comment'>
另外,您不希望将控件用作模型。模型是您想要维护值的地方,因此它应该是

    [(ngModel)] = 'commentValue'>

如果要将控件与输入关联,则需要使用
NgFormControl
指令:

<ion-input type="text" placeholder="your comment" 
           [(ngModel)] = "comment"
           [ngFormControl]="this.form.controls['comment']">
</ion-input>
有关更多详细信息,请参阅本文:


    • 以下是我在项目中所做的:

      • 在HTML中:

      从视图更新到控制器,对吗?我希望以完全相同的方式从视图到控制器更新,而不是相反的方式,但是this.comment是空字符串,但是如果您从未设置值,则不能期望它有一个值。使用
      ngModel
      this.comment
      中绑定控件是错误的<代码>(ngModel)=“xxx”不起任何作用,因为如果不使用
      [(xxx)]
      您必须使用
      (ngModelChange)=“comment”
      <在这种情况下,code>comment应该是一个表达式,因此您可能希望
      (ngModelChange)=“comment=$event”
      。另一种方法可能是使用
      'comment':['initialValue',Validators.required]
      传递初始值。为什么必须从addcomment(form.value)中取消定义此“console.log(form.value)”,您需要利用
      ngFormModel`指令。在您的例子中,您使用
      FormBuilder
      …@Thiery Templier将内联表单定义和显式表单定义混合在一起,我真的需要您的帮助
          [(ngModel)] = 'commentValue'>
      
      <ion-input type="text" placeholder="your comment" 
                 [(ngModel)] = "comment"
                 [ngFormControl]="this.form.controls['comment']">
      </ion-input>
      
      <form [ngFormModel]="form">
        (...)
      </form>
      
      <form #myForm="ngForm" (ngSubmit)='addComment(myForm)'>
          <ion-input type="text" placeholder="your comment" [(ngModel)]='model.comment' #comment="ngForm" required></ion-input>
          <button [disabled]="!myForm.form.valid">add comment</button>
      </form>