1
我需要在角2双向更新模型
我更新的方法来更新我的模型时,在角2结合样子:
putDep(item) {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
this.selected.departmentName = item.departmentName;
this.selected.departmentLocation = item.departmentLocation;
return this.http.put('http://localhost:65402/company/api/department'+ "/update/" + this.selected.departmentNo, JSON.stringify(item),{headers: headers})
.subscribe(res => {this.departments = res.json();});
}
和HTML代码:
<form #selected="ngForm" [hidden]="!showEditView" align="center">
<div>
<label for="editAbrv">Department No:</label><br>
<input ngControl="departmentNo" placeholder="name">
</div>
<div>
<label for="editAbrv">Department name:</label><br>
<input ngControl="departmentName" placeholder="name">
</div>
<div>
<label for="editAbrv">Department Location:</label><br>
<input ngControl="departmentLocation" placeholder="location">
</div>
<div>
<a href="javascript:void(0);" (click)="putDep(selected.value)" title="Add">
Save
</a>
<a href="javascript:void(0);" (click)=showHide($event) >
Cancel
</a>
</div>
</form>
问题出在2种方式与模型绑定。这是它的外观在后端:
[HttpPut]
[Route("Update/{id}")]
public async Task<HttpResponseMessage> Update(int id, DepartmentModel model)
的问题是,当我点击编辑模式,我INT ID传递到后端,但我的模型ID(应当网元一样传入ID)为null 。另外,当我点击编辑按钮时,我无法看到在为模型输入值时如何改变视图。在角1我快速解决它,但在角2是斗争。关键是,我想要id自动连接到我的模型,当我点击编辑,我可以看到它在输入字段(以及其他2个属性)。感谢帮助!
你的意思是[(ngModel)],而不是[(ngForm)],某事像这样:[(ngModel)] =“selected.departmentNo :)我也试过,但问题是当我点击我的编辑按钮时,在表单输入字段中没有来自我的表格行的值。在Angular 1中很容易,不知道是什么问题。 –
我得到的错误:TypeError:无法在AppComponent @ 76:32的[selected.departmentNo]中读取undefined属性'departmentNo' –