错误TS2339:类型“ HTMLElement”上不存在属性“名称”

时间:2018-12-14 11:05:56

标签: javascript jquery html angular

profile.component.ts:

var $inputs = $('#changePasswordForm :input');
    var values = {
      oldpassword: String,
      newpassword: String
    };
    $inputs.each(function() {
      console.log(this);
      values[this.name] = $(this).val();
    });
    console.log(values.oldpassword, this.currentUser.username);

profile.component.html:

<form id='changePasswordForm'>
            <div class='form-group input-field'>
              <label for=''>Old Password*</label>
              <input type='password' [(ngModel)]='oldpassword' name='oldpassword' class='form-control'>
            </div>
            <div class='form-group input-field'>
              <label for=''>New Password*</label>
              <input type='password' [(ngModel)]='newpassword' name='newpassword' class='form-control'>
            </div>
          </form>

错误:

src / app / components / profile / profile.component.ts(82,19)中的错误:错误TS2339:类型“ HTMLElement”上不存在属性“名称”。

我怎么了?

1 个答案:

答案 0 :(得分:0)

  
    

尽量不要在Angular中使用JQuery

  

您可以在Angular(ngx)中执行这样的代码。 您正在使用ngModel,因此它是两种方式的绑定,您可以在组件文件中访问此值。

HTML

<form id='changePasswordForm' (ngSubmit)="changePass()">
            <div class='form-group input-field'>
              <label for=''>Old Password*</label>
              <input type='password' [(ngModel)]='oldpassword' name='oldpassword' class='form-control'>
            </div>
            <div class='form-group input-field'>
              <label for=''>New Password*</label>
              <input type='password' [(ngModel)]='newpassword' name='newpassword' class='form-control'>
            </div>

            <button type="submit">Change Password</button>
</form>

TS(组件)

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  oldpassword: string;
  newpassword: string;

  changePass() {

// Here we are getting and setting values
    const values = {
        oldpassword: this.oldpassword,
        newpassword: this.newpassword
    };

    console.log(values);
  }
}

https://stackblitz.com/edit/ngx-change-password-demo