简体   繁体   English

角度6:错误TS2339:类型“ HTMLElement”上不存在属性“值”

[英]Angular 6: error TS2339: Property 'value' does not exist on type 'HTMLElement'

I have textarea which allow user to submit comments, I want to grab the date on time the comment is submitted, and save to json together with comment added : 我有允许用户提交评论的textarea,我想按时抓取评论提交的日期,并与添加的评论一起保存到json中:

after comment is submitted in json file I would like to have something like this: 在json文件中提交评论后,我想要这样的东西:

 "comment": [
    {
      "id": 1,
      "username": "Michael Ross",
      "city": "New York USA",
      "date": "2018-01-01T00:00:00",
      "task_id": 1,
      "description": "Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis, malesuada ultricies. Curabitur et lig"
    }
]

Problem: Right now when comment is submitted I have the following: no date is displayed: 问题:现在提交评论时,我有以下内容:没有显示日期:

 "comment": [
    {
      "id": 1,
      "username": "Michael Ross",
      "city": "New York USA",
      "task_id": 1,
      "description": "Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis, malesuada ultricies. Curabitur et lig"
    }
]

Here is what I have tried so far to grab the date from entered comment. 到目前为止,这是我尝试从输入的评论中获取日期的方法。

HTML : HTML:

<form class="add-comments" [formGroup]="addForm" (keyup.enter)="addComments()">
      <input type="hidden" id="localTime" name="localTime">
    <div class="form-group">
      <textarea class="form-control" rows="1" placeholder="Add comments" formControlName="description" id="description"></textarea>
    </div>
  </form>

Here is method on compoents ts. 这是有关组件ts的方法。

 addComments(task_id) {
    const formData = this.addForm.value;
    formData.task_id = task_id;
    this.userService.addComments(formData)
    .subscribe(data => {
      this.comments.push(this.addForm.value);
    });
    const date = new Date();
    const d = date.getUTCDate();
    const day = (d < 10) ? '0' + d : d;
    const m = date.getUTCMonth() + 1;
    const month = (m < 10) ? '0' + m : m;
    const year = date.getUTCFullYear();
    const h = date.getUTCHours();
    const hour = (h < 10) ? '0' + h : h;
    const mi = date.getUTCMinutes();
    const minute = (mi < 10) ? '0' + mi : mi;
    const sc = date.getUTCSeconds();
    const second = (sc < 10) ? '0' + sc : sc;
    const loctime = month + day + hour + minute + year + '.' + second;

    document.getElementById('localTime').value = loctime;

  }

Unfortunatelly when I submit the comment , I get the following error 不幸的是,当我提交评论时,出现以下错误

ERROR in src/app/user-profile/user-profile.component.ts(75,21): error TS2365: Operator '+' cannot be applied to types 'string | number' and 'string | number'.
src/app/user-profile/user-profile.component.ts(77,42): error TS2339: Property 'value' does not exist on type 'HTMLElement'.

what do I need to change to get what I want?? 我需要更改以获得我想要的东西吗?

You can try this code 您可以尝试此代码

const loctime = `${year}-${month}-${day}T${hour}:${minute}:${second}`;
// output "2018-10-27T10:26:32"

Instead of using Javascript Way, try to use Angular way 代替使用Javascript方式,尝试使用Angular方式

<input type="hidden" id="localTime" name="localTime" formControlName="localTime">

this. addForm.get('localTime').setValue(loctime);

Note: we need to use back-tick(``) instead of single quote (''). 注意:我们需要使用反勾号(``)而不是单引号('')。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TS2339:类型“ HTMLElement”上不存在属性“ imageID” - error TS2339: Property 'imageID' does not exist on type 'HTMLElement' 错误TS2339:类型“ HTMLElement”上不存在属性“名称” - error TS2339: Property 'name' does not exist on type 'HTMLElement' Angular:TS2339:“HTMLElement”/“HTMLTextAreaElement”/等/等类型上不存在“之前”属性 - Angular: TS2339: Property 'before' does not exist on type 'HTMLElement' / 'HTMLTextAreaElement' / etc / etc 如何解决错误“TS2339:&#39;JQuery 类型上不存在属性&#39;仪表&#39;<HTMLElement> &#39;。” - How do I resolve error "TS2339: Property 'gauge' does not exist on type 'JQuery<HTMLElement>'." 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** EventTarget 类型上不存在属性“值”(ts2339) - Property 'value' does not exist on type EventTarget (ts2339) 错误TS2339:类型“ {}”上不存在属性“ forEach” - error TS2339: Property 'forEach' does not exist on type '{}' 错误 TS2339:“温度”类型上不存在属性“摄氏度” - error TS2339: Property 'celsius' does not exist on type 'Temperature' 错误 TS2339:“特殊 []”类型上不存在属性“默认” - Error TS2339: Property 'default' does not exist on type 'Special[]' Angular2打字稿错误TS2339:类型“ Y”上不存在属性“ x” - Angular2 typescript error TS2339: Property 'x' does not exist on type 'Y'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM