【发布时间】:2016-10-21 07:52:16
【问题描述】:
<div class="form-horizontal" style="margin:auto;" *ngFor="#cmp of getUser">
<div class="form-group">
<label class="col-md-3 control-label">Name:</label>
<div class="col-md-8">
<input type="text" class="form-control" id="cname" [(ngModel)]="cmp.name"/>
</div>
</div>
<button class="slds-button slds-button--neutral slds-button--brand" (click)="updateUser(cmp)">Save</button>
</div>
在这里,通过在 ngFor 中使用 ngModel,我可以显示数据,但是如果我想更新/编辑此显示数据,那该怎么做呢?
updateUser(){
var body=JSON.stringify({
name:this.cmp.name,
//what to do here????
})
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
this.http.post('../update.php?id='+matches['id'],body,{
headers:headers;
})
.map(res => res.json())
.map((res) => {
if (res.success) {
this.message = res.message;
}
else{
this.err="Something went wrong";
}
})
.subscribe(
data=>this.updUser=JSON.stringify(data),
error=>alert(error),
msg=>console.log("done"));
}
在这个 updateUser 函数中,我想更新数据。但是当我这样做时,它向我显示了 cmp 未定义的错误。那么如何在 post 方法中使用来自 [(ngModel)]=cmp.name 的值???
【问题讨论】:
-
您使用的是哪个版本的 Angular 2?在最新版本中,它应该是
let cmp of getUser而不是#cmp。 -
@AlexanderCiesielski 我正在使用 angular2: 2.0.0-beta.14 版本