我想添加带角度js前端和symfony(后端)的用户,我这个错误和选择器中的数据是emty。可能有人知道问题是什么
AddUserComponent.html:16 ERROR TypeError:无法读取属性 '用户名'未定义的 at Object.eval [as updateRenderer](AddUserComponent.html:20) at Object.debugUpdateRenderer [as updateRenderer](core.es5.js:13105) 在checkAndUpdateView(core.es5.js:12256) at callViewAction(core.es5.js:12599) 在execComponentViewsAction(core.es5.js:12531) 在checkAndUpdateView(core.es5.js:12257) at callViewAction(core.es5.js:12599) at execEmbeddedViewsAction(core.es5.js:12557) 在checkAndUpdateView(core.es5.js:12252) at callViewAction(core.es5.js:12599)View_AddUserComponent_0 @ AddUserComponent.html:16 AddUserComponent.html:16 ERROR CONTEXT DebugContext _
add-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() {
}
}
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);
}
答案 0 :(得分:1)
HTML ngModel
中存在拼写错误。改变
[(ngModel)]="usernamee"
到
[(ngModel)]="username"