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”类型上不存在属性“name”。
我做错了什么??
尽量不要在 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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句