【问题标题】:How to send the value of web api in hidden form fields in Angular如何在 Angular 的隐藏表单字段中发送 web api 的值
【发布时间】:2019-08-31 20:22:15
【问题描述】:

我有一个从 web api 获取数据的 Angular 应用程序。我的页面中还有一个表单,它有一个隐藏字段,需要从该 api 发送值:number。我已经尝试了以下代码,但它不起作用:

ts.component 文件。

receivedIncident: any;

number: any;

constructor(private service: nowService,
        private appComponent: AppComponent,
        private userService: UserService,
        private router: Router,
        private http: HttpClient,
        private route: ActivatedRoute
      ) {
        this.receivedIncident = { number: '', opened_at: '', description: '', short_description: ''}; this.receivedLocation = {city:null, country: null}
      }

    private getIncident() {
        this.service.getIncident(this.s_id, this.c_id).subscribe((data) => {
          this.loading = true;
          console.log('Result - ', data);
          console.log('incident data is received');
          this.loading = true;
          this.receivedIncident = data.result[0];

        })
      }

ngOnInit() {
    this.loading = true;
    this.getIncident();
    this.loading = true;
      })
      this.addCommentsForm = new FormGroup({
        comment: new FormControl(''),
        c_id: new FormControl(this.customer_id),
        number: new FormControl(this.receivedIncident.number),
      })
    }

html 表单域

<input name="number" class="form-input" type="hidden" id="number" value="number

有什么想法吗?

【问题讨论】:

  • 在调用 API 之前先创建表单。如果您的输入在表单中,则将formControlName='number' 添加到输入中,如果不是,则将formControl 作为输入属性添加到输入
  • 你能详细说明吗?
  • @Sole 检查我的答案如下

标签: angular


【解决方案1】:

我遇到了您在此处提到的确切问题。为了做那件事,你可以使用 setValue 方法。它完全符合您的要求。 这是文件:

https://angular.io/api/forms/FormControl

这是一个简单的答案:

Manually Set Value for FormBuilder Control

我希望它对你有用。

【讨论】:

    【解决方案2】:

    您应该在 html 模板的输入字段中添加 formControlName。

    <input name="number" class="form-input" formControlName="number" id="number" value="number [hidden]="true"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 2013-07-08
      • 2013-08-29
      • 2013-04-06
      • 2016-08-28
      • 2014-06-11
      • 1970-01-01
      相关资源
      最近更新 更多