html 文件中某些属性未定义错误 angular
2019-06-19
152
我在 user.component.html 中有以下代码,控制台显示: ERROR TypeError:无法读取未定义的属性“first_name”。
但该属性将输入与我所期望的数据正确绑定,但错误仍然出现。
我认为该属性在数据到达控制器之前尝试绑定,但我不知道发生了什么。
我在 angular 方面比较陌生。
<button type="button" class="btn btn-outline-primary" (click)="regresarHome()">Regresar</button>
<div class="row">
<div class="card offset-3 col-md-6">
<div class="card-body">
<form>
<div class="form-group">
<label for="first_name">Nombre</label>
<input type="text" class="form-control" id="first_name" name="first_name" placeholder="Ingresa tu nombre " value="{{_user.first_name}}" #first_name>
</div>
<div class="form-group">
<label for="last_name">Apellidos</label>
<input type="text" class="form-control" id="last_name" name="last_name" placeholder="Ingresa tu(s) apellidos " value="{{_user.last_name}}" #last_name>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email" value="{{_user.email}}" #email>
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="user_active" [checked]="_user.active == 1" #active>
<label class="form-check-label" for="exampleCheck1">Usuario Activo</label>
</div>
<div class="form-group">
<label for="username">Nombre de usuario</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Ingresa tu nombre de usuario" value="{{_user.username}}" #username>
</div>
<div class="form-group">
<label for="password">Contraseña</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password" value="{{_user.password}}" #password>
</div>
<button type="button" class="btn btn-outline-primary btn-block" (click)="guardarUsuario(first_name.value,last_name.value,email.value,active.checked,username.value,password.value)">Guardar</button>
</form>
</div>
</div>
</div>
我的控制器代码如下:
import { Component, OnInit } from '@angular/core';
import { SakilaService } from '../../Services/sakila.service';
import { Router, ActivatedRoute } from '@angular/router';
import { IUser } from '../../Models/IUser';
import { formatDate } from '@angular/common';
@Component({
selector: 'app-user',
templateUrl: './updateUser.component.html'
})
export class UpdateUserComponent implements OnInit {
_user_id: number;
_user: any;
constructor(private sakilaService: SakilaService,
private router: Router,
private _activatedRoute: ActivatedRoute) {
this._activatedRoute.params.subscribe(params => {
this._user_id = params.user_id;
});
if (this._user_id !== 0) {
this.sakilaService.getUserById(this._user_id)
.subscribe(
(data) => {
this._user = data.data;
}
);
}
}
ngOnInit() { }
regresarHome() {
this.router.navigate(['home']);
}
guardarUsuario(first_name: string, last_name: string, email: string, active: boolean, username: string, password: string) {
let usuario: IUser = {} as IUser;
usuario.staff_id = this._user_id;
usuario.first_name = first_name;
usuario.last_name = last_name;
usuario.address_id = 1;
usuario.picture = null;
usuario.email = email;
usuario.store_id = 1;
usuario.active = active;
usuario.username = username;
usuario.password = password;
usuario.last_update = formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss', 'en-us');
this.sakilaService.updateUser(usuario);
}
}
IUser 的代码是:
export interface IUser {
staff_id?: number;
first_name: string;
last_name: string;
address_id: number;
picture: Blob;
email: string;
store_id: number;
active: boolean;
username: string;
password: string;
last_update: string;
}
1个回答
user2216584
2019-06-19