【问题标题】:'setValue' method is not working in Angular application'setValue' 方法在 Angular 应用程序中不起作用
【发布时间】:2019-03-09 07:51:16
【问题描述】:

我正在使用带有 Angular 6 的 MEAN Stack 开发一个 Web 应用程序。我的 html 中有一个按钮来获取默认值,当我单击该按钮时,它应该使用默认值填充表单字段。这是我的 html 表单。

    <div style="float: right" id="extrudedHeightPanel" *ngIf="isExtrudedHeight" name="extrudedHeight">
      <form #extrudedHeightForm="ngForm" (ngSubmit)="saveExtrudedHeightValue(extrudedHeightForm)" #form="ngForm">
        <nb-card class="action-buttons">
          <div class="form-input-group">
            <div class="row">
              <div class="">
                <button type='button' (click)="setDefaultValues()" class="btn btn-sm btn-rectangle btn-default text-case">Restore Default</button>
              </div>
              <div class="">
                <button type="submit" class="btn btn-sm btn-rectangle btn-default text-case">Done</button>
              </div>
            </div>
          </div>
        </nb-card>
        <br>
        <br>
        <br>
        <p>Extruded Height</p>
        <br>
        <div class="form group">
          Extruded Height:
          <input type="text" nbInput name="extrudedHeight" [(ngModel)]="extrudedHeight" />
        </div>
      </form>
    </div>

我从 mongo db 获取数据到我的 .ts 文件,并尝试使用 Angular 中的“setValue”方法为表单字段设置值。 .ts 文件

    class{

        extrudedHeightForm: FormGroup;
        ngOnInit()
        {
        this.extrudedHeightForm = new FormGroup({
              extrudedHeight: new FormControl()
            });
        }

                  //Set default values for extrudedHeight
                  setDefaultValues() {
                    this.extrudedHeightService.getExtrudedHeight("default").subscribe(data => {
                      this.extrudedHeightForm.setValue({
                        extrudedHeight:data['extrudedHeight']
                      });
                    });
                  }

   }

我的问题是以下部分不起作用。我错了还是有什么方法可以满足我的要求。

this.extrudedHeightForm.setValue({
                            extrudedHeight:data['extrudedHeight']
                          });

--已更新-- 当我变成this.extrudedHeightForm.get('extrudedHeight').setValue(data['extrudedHeight']); 正如答案中所建议的那样,它也不起作用。为了检查值,我打印了一个 console.log。 'console.log(this.extrudedHeightForm.get('extrudedHeight'));'部分给出以下值。

但值 250 未显示在表单字段中。

【问题讨论】:

  • 它不起作用,但它有什么作用?没有? data['extrudedHeight'] 的值是否改变了?订阅块触发了吗?
  • 补丁值也不起作用
  • @danday74 data['extrudedHeight'] 的值是相同的。当我尝试调试时,我什至无法为“extrudedHeight:data['extrudedHeight']”行设置断点
  • 如果值相同,那么当您设置该值时,什么都不会改变。如果无法设置断点,请使用 console.log。

标签: node.js angular mean-stack


【解决方案1】:

试试

this.extrudedHeightForm.get('extrudedHeight').setValue(data['extrudedHeight']);

您应该在 FormControl 上设置值,而不是在 FormGroup 上。

【讨论】:

  • 感谢您的建议。我应用了它但没有用。我根据你的回答更新了问题
【解决方案2】:

在初始化表单控件的时候,我想你需要给它一个类型的提示,比如:

       this.extrudedHeightForm = new FormGroup({
          extrudedHeight: new FormControl('')
        });

下面的代码应该适合你(我已经删除了一些不必要的部分):

    <div style="float: right" id="extrudedHeightPanel" >
    <form [formGroup]="extrudedHeightForm"  (ngSubmit)="saveExtrudedHeightValue(extrudedHeightForm)">
      <div class="">
        <button type='button' (click)="setDefaultValues()" class="btn btn-sm btn-rectangle btn-default text-case">Restore Default</button>
      </div>
      <br>
      <br>
      <br>
      <p>Extruded Height</p>
      <br>
      <div>
        Extruded Height:

        <input type="text" formControlName="extrudedHeight" [(ngModel)]="extrudedHeightForm.extrudedHeight"/>
      </div>
    </form>
  </div>

【讨论】:

  • 我已经编辑了答案,这次看看是否有帮助。
【解决方案3】:

您使用的是两种完全不同的形式。在您的模板中,您有一个模板驱动的表单,而在您的 TS 中,您有一个反应式表单,所以当然,当您在 FormControl 中设置值时,它不会反映在您的视图中。您需要在模板中实际使用您的反应式表单。这是一个简化的版本:

<form [formGroup]="extrudedHeightForm">
  <input formControlName="extrudedHeight" />
</form>

表单的构建看起来与您在 ts 中的相同,即:

ngOnInit() {
  this.extrudedHeightForm = new FormGroup({
    extrudedHeight: new FormControl()
  });
}

并设置值:

this.extrudedHeightService.getExtrudedHeight("default").subscribe((data: any) => {
  this.extrudedHeightForm.get('extrudedHeight').setValue(data.extrudedHeight);
});

在上面,不要使用any 作为类型,而是将你的数据作为接口或类来输入。

【讨论】:

  • 感谢您的回答。由于您的回答,我得到了一些理解。
  • @rosd 我看到您同时使用 ngModel 和 formcontrolname 接受了答案。只想提一下,这已被弃用,并将在 Angular 版本 7 中删除。ngModel 和 formcontrol 不应该一起使用,将来您将不得不选择模板驱动或响应式表单:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2019-01-15
  • 2017-01-24
  • 2021-11-17
  • 2018-03-22
  • 2019-01-23
相关资源
最近更新 更多