Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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
Forms 为什么ngModelChange会发出以前的输入值?_Forms_Angular_Input_Reactive - Fatal编程技术网

Forms 为什么ngModelChange会发出以前的输入值?

Forms 为什么ngModelChange会发出以前的输入值?,forms,angular,input,reactive,Forms,Angular,Input,Reactive,我有一个(非常)简单的Angular2组件,只有一个文本字段。我希望这样,每当对该字段进行任何更改时,控制台都会记录该字段的值。即如果有人键入“ABCD”,控制台应记录:“A”、“AB”、“ABC”、“ABCD” 然而,我看到的情况是,在注册新输入之前,控制台正在记录字段的先前值。因此,我得到的是:“,”A“,”AB“,”ABC“ 如何使控制台在输入新字符后记录文本字段的值 多谢各位 到目前为止,我的代码是: import { Component, OnInit, ViewChild, Even

我有一个(非常)简单的Angular2组件,只有一个文本字段。我希望这样,每当对该字段进行任何更改时,控制台都会记录该字段的值。即如果有人键入“ABCD”,控制台应记录:“A”、“AB”、“ABC”、“ABCD”

然而,我看到的情况是,在注册新输入之前,控制台正在记录字段的先前值。因此,我得到的是:“,”A“,”AB“,”ABC“

如何使控制台在输入新字符后记录文本字段的值

多谢各位

到目前为止,我的代码是:

import { Component, OnInit, ViewChild, EventEmitter, Output } from '@angular/core';
import { NgForm } from '@angular/forms';

@Component({
  selector: 'modal-test-form',
  template: `
    <form>
      <input name="data" placeholder="Type something" ngModel (ngModelChange)="onTextChange()"/>
    </form>
  `,
  //templateUrl: './modal-test-form.component.html',
  styleUrls: ['./modal-test-form.component.css']
})
export class ModalTestFormComponent{
  @ViewChild(NgForm) testForm : NgForm;

  constructor() { }

  onTextChange() {
    console.log(this.testForm.form.value);
  }

}
从'@angular/core'导入{Component,OnInit,ViewChild,EventEmitter,Output};
从'@angular/forms'导入{NgForm};
@组成部分({
选择器:'模态测试表',
模板:`
`,
//templateUrl:“./modal test form.component.html”,
样式URL:['./模态测试表单.component.css']
})
导出类ModalTestFormComponent{
@ViewChild(NgForm)testForm:NgForm;
构造函数(){}
onTextChange(){
log(this.testForm.form.value);
}
}

将方法从

(ngModelChange)="onTextChange()" 
根据表格中的console.log值,我认为您在提交问题(??)时写了一个“错误”,实际上您的意思是在html中有这个方法:

(ngModelChange)="emitDataValue()" 
但无论如何,请将其更改为:

(keyup)="emitDataValue()"
它似乎按照您希望的方式工作。

将您的方法更改为

(ngModelChange)="onTextChange($event)"
和在组件类中

onTextChange(val: string): any {
  console.log("updated value is --->", val);
}

什么是“旧值”?@AJT_82那么现在发生的是console.log(this.testForm.form.value)在输入新字符之前记录表单的值。因此,如果我刚刚输入“ABC”,控制台将只记录“AB”。我希望它改为记录“ABC”,这是当前值。这就解决了它!非常感谢。