ERROR TypeError: Cannot read property 'username' of undefined
ERROR TypeError: Cannot read property 'username' of undefined
我想用 angular js 前端和 symfony(后端)添加用户,我这个错误,我在选择器中的数据是空的。有人知道是什么问题吗
AddUserComponent.html:16 ERROR TypeError: Cannot read property
'username' of undefined
at Object.eval [as updateRenderer] (AddUserComponent.html:20)
at Object.debugUpdateRenderer [as updateRenderer] (core.es5.js:13105)
at checkAndUpdateView (core.es5.js:12256)
at callViewAction (core.es5.js:12599)
at execComponentViewsAction (core.es5.js:12531)
at checkAndUpdateView (core.es5.js:12257)
at callViewAction (core.es5.js:12599)
at 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
添加用户
<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() {
}
}
用户服务
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);
}
您的 HTML ngModel
中有错字。改变
[(ngModel)]="usernamee"
到
[(ngModel)]="username"
我想用 angular js 前端和 symfony(后端)添加用户,我这个错误,我在选择器中的数据是空的。有人知道是什么问题吗
AddUserComponent.html:16 ERROR TypeError: Cannot read property 'username' of undefined at Object.eval [as updateRenderer] (AddUserComponent.html:20) at Object.debugUpdateRenderer [as updateRenderer] (core.es5.js:13105) at checkAndUpdateView (core.es5.js:12256) at callViewAction (core.es5.js:12599) at execComponentViewsAction (core.es5.js:12531) at checkAndUpdateView (core.es5.js:12257) at callViewAction (core.es5.js:12599) at 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
添加用户
<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() {
}
}
用户服务
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);
}
您的 HTML ngModel
中有错字。改变
[(ngModel)]="usernamee"
到
[(ngModel)]="username"