我有一项服务,可以在数据库中添加新记录。
<div class="container">
<div class="resultrow">
<div id="resultMap" class="col-md-4 no-float">
<div id="GoogleResultMap" style="width:100%;border: 1px solid black;"></div>
</div>
<div id="resultContent" class="col-md-8 no-float">
<div id="yogaSpaceList">
<div id="theList" style="border: 1px solid black;overflow-y:scroll;">
@foreach (var space in Model.YogaSpaces) {
<div>
<h4>@space.Overview.Title</h4>
<div>
@space.Overview.Summary
</div>
</div>
<hr />}
</div>
<div class="pagedList">
@Html.PagedListPager(Model.YogaSpaces, page => Url.Action("Search", new { Model.SearchQuery, Model.ClassDate, page }), PagedListRenderOptions.MinimalWithItemCountText)
</div>
</div>
</div>
</div>
</div>
res.data中的值是新的主键号。这将返回到控制器,如下所示:
return $http.post('/models/CreateModel', userObj)
.then(function (res)
{
// Return the new model_id
return res.data;
},
function (err)
{
// console.log("THERE WAS AN ERROR");
});
当我使用console.log打印$ scope.newID时,我发现它包含一个对象而不是值。该对象在我的控制台中如下所示:
$scope.newID = ModelService.addModel(xMdl);
我如何访问值232,因为我需要更新角度模型中的id?
答案 0 :(得分:1)
你在方法中返回prommise
return $http.post('/models/CreateModel', userObj)
在这种情况下,你必须以下一种方式重写它:
//your controller
save = function(){
var self=this;
this.service.createModel(this.$scope.mmodel)
.then(function (res)
{
self.$scope.neId = res.data;
},
function (err)
{
// console.log("THERE WAS AN ERROR");
});
}
//service
createModel = function(model){
return $http.post('/models/CreateModel', userObj);
};