将 textarea 和 elementId 传递给函数的最佳方法

时间:2021-07-08 21:25:05

标签: javascript html angular typescript

我是一个 UI/Web 菜鸟,所以请耐心等待。 我有一个 html textarea 元素,它具有动态 json 验证(我不知道这是否会影响元素,而不是使元素混乱)。我现在需要通过单击按钮将选中的值传递给打字稿函数,以便它可以发送到 api 以针对模型进行验证。我有 5 个需要此功能的 textarea 框,并且希望有一组代码来传递 textarea 值和元素的 id(与 api 模型类型相关)。我认为最接近的是发送 http 请求,但参数都为空。当前错误为:

core.js:6456 ERROR TypeError: Cannot read property 'value' of null

我正在使用: 角 CLI:12.0.3 节点:14.16.0 包管理器:npm 6.14.11 TSC 4.2.4 版

HTML:

  <div class="form-group" [formGroup]="form">
    <label for="siteConfiguration">SiteConfiguration</label><br />
    <textarea rows="6" cols="80"
              class="form-control"
              id="'siteConfiguration'"
              name="siteConfiguration"
              [formControlName]="'siteConfiguration'"
              [(ngModel)]="siteCA.siteConfiguration"
              ngModel="jsonconfiguration"></textarea>
    <input type="button" value="Validate" style="width: auto" id="jsonConfiguration" (click)="GetCheckJsonConfiguration(id, siteCA.siteConfiguration)" /><br />
    </div>
    <pre></pre>
    <div id="jsonvalidatortext" [hidden]="form.get('siteConfiguration').pristine || form.get('siteConfiguration').valid">
      Please insert valid JSON.
    </div>
    <pre></pre>

打字稿:

  GetCheckJsonConfiguration() {
    const jsonConfigurationName = 'siteconfiguration'; 
    const jsonConfiguration: any = (document.getElementById('siteconfiguration') as HTMLInputElement).value;
  this._checkjsonconfigurationservice.getCheckJsonConfiguration(jsonConfiguration, jsonConfigurationName)
    .subscribe(data => this.result = data);
  console.log(this.result);
  this.cdf.detectChanges();
  };

总结: 初始页面加载后,将填充 textarea。文本区域应该有一个按钮,将文本区域的内容与元素 Id 一起发送到函数。该函数调用 api 并返回一个包含 true/false/errmsg 的字符串,并应显示在 textarea 上方。

感谢您的关注。

0 个答案:

没有答案