错误TypeError:无法读取undefined的属性'username'

问题描述 投票:0回答:1

我想添加角度js前端和symfony(后端)的用户,我这个错误和选择器中的数据是emty。可能有人知道是什么问题

AddUserComponent.html:16 ERROR TypeError:无法在checkAndUpdateView的Object.debugUpdateRenderer [as updateRenderer](core.es5.js:13105)的Object.eval [as updateRenderer](AddUserComponent.html:20)中读取未定义的属性'username' (core.es5.js:12256)在callViewAction(core.es5.js:12599)的execComponentViewsAction(core.es5.js:12531),在checkAndUpdateView(core.es5.js:12257)的callViewAction(core.es5.js) :12599)execEmbeddedViewsAction(core.es5.js:12557)at checkAndUpdateView(core.es5.js:12252)at callViewAction(core.es5.js:12599)View_AddUserComponent_0 @ AddUserComponent.html:16 AddUserComponent.html:16 ERROR CONTEXT DebugContext_

附加user.component

Add User

  <div class="form-group">
    <label>username:</label>
    <input type="text" class="form-control" placeholder="add username" [(ngModel)]="usernamee" name="username" >
  </div>
  <div class="form-group">
    <label>Email:</label>
    <input type="text" class="form-control" placeholder="add email" [(ngModel)]="emaill" name="email" >
  </div>
<select>
  <option ng-repeat="user in users.result">{{user.username}}</option>
  </select>

      <div *ngFor="let error of errors" class="alert alert-danger">
          <div>There is an error in :{{error.field}} field</div>
          <div>{{error.message}}</div>
        </div>

  <button type="submit" class="btn btn-primary" >Save</button>

</form>

附加user.component

 import { Component, OnInit } from '@angular/core';
    import {UserService} from '../user.service';
    import {Router} from '@angular/router';

    @Component({
      selector: 'app-add-post',
      templateUrl: './add-user.component.html',
      styleUrls: ['./add-user.component.css']
    })
    export class AddUserComponent implements OnInit {

      username: string ;
      email: string;

      errors= [];


      constructor(private _userService: UserService , private router: Router) { }

      addUser(username, email) {

        let user: any;
        user = {username: username, email: email};
        this._userService.addUser(user).subscribe(( result => {

          this.router.navigate(['/users']);

        }), addError => this.errors = addError);

      }


  ngOnInit() {
  }

}

Userservice

addUser(user: User) {
        const  headers = new Headers();
        headers.append('content-type', 'application/json');
        headers.append('Authorization', 'Bearer ' + this.authenticationService.token);
        return this.http.post(this.uri, JSON.stringify(user), {headers : headers}).map(res => res.json()).catch(this.handelError);
      }
angularjs symfony-3.3
1个回答
1
投票

你的HTML ngModel中有一个拼写错误。更改

[(ngModel)]="usernamee"

[(ngModel)]="username"
© www.soinside.com 2019 - 2024. All rights reserved.