html文件angular

时间:2019-06-19 16:10:09

标签: angular

在控制台中,我在user.component.html中具有以下代码:错误TypeError:无法读取未定义的属性'first_name'。

但是该属性将输入与我指定的数据正确绑定,但是错误仍然出现。

我认为该属性会在数据到达控制器之前尝试绑定,但是我不知道发生了什么。

我的角度比较新。

<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 个答案:

答案 0 :(得分:1)

像这样在模板中使用安全的运算符:

value="{{_user?.first_name}}"

有关参考,请参见以下SO答案-Reading an environment variable before it is defined