Angularjs 承诺返回不更新范围
我创建了一个服务来更新列表。执行插入的调用工作正常,但是当数据返回并且我尝试更新列表时,我得到的错误Angularjs 承诺返回不更新范围,angularjs,Angularjs,我创建了一个服务来更新列表。执行插入的调用工作正常,但是当数据返回并且我尝试更新列表时,我得到的错误undefined不是一个函数。这并没有真正告诉我问题在哪里,我被难住了 这是控制器中的调用 $scope.createCategory = function (newCategory) { CategoryService.createCategory(newCategory) .then(function(category){ $scope.cat
undefined不是一个函数。这并没有真正告诉我问题在哪里,我被难住了
这是控制器中的调用
$scope.createCategory = function (newCategory) {
CategoryService.createCategory(newCategory)
.then(function(category){
$scope.categoryNames.push({
asset_category_id: category.id , asset_type: category.name}
);
});
};
以下是服务:
createCategory: function(name){
var params = 'categoryName' + '=' + name.categoryName + '&';
var defObj = $q.defer();
params += 'method' + '=' + 'createCategory';
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
$http.post('cfc/category.cfc', params);
$http.success(function(data){
defObj.resolve(data);
});
return defObj.promise;
}
我猜问题出在。然后,但我不完全确定。有什么建议吗
添加视图:
<div class="panel">
<div class="col-xs-6">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Category Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="category in categoryNames">
<td>{{$index + 1}}</td>
<td>{{category.asset_type}}</td>
<td>
<span class="glyphicon glyphicon-wrench" data-ng-click="editCategory($index)"></span>
</td>
<td>
<span class="glyphicon glyphicon-remove" data-ng-click="removeCategory($index)"></span>
</td>
</tr>
</tbody>
</table>
</div>
<div class="col-xs-6">
<div class="well">
<div class="h4">Create Category</div>
<div class="well">
<form role="role" data-ng-submit="createCategory(newCategory)">
<div class="form-group">
<label for="categoryName">Category Name</label>
<input type="text" class="form-control" id="categoryName" name="categoryName" placeholder="Category Name" data-ng-model="newCategory.categoryName"/>
</div>
<button type="submit" class="btn btn-primary btn-block">Add</button>
</form>
</div>
</div>
</div>
#
类别名称
编辑
删除
{{$index+1}}
{{category.asset_type}
创建类别
类别名称
添加
首先,你甚至不需要$q
createCategory: function(name){
var params = 'categoryName' + '=' + name.categoryName + '&';
params += 'method' + '=' + 'createCategory';
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
return $http.post('cfc/category.cfc', params);
}
其次,您可能需要使用$scope.$apply,或者不使用
.then(function(category){
$scope.categoryNames = category;
$scope.$apply('categoryNames');
});
以下几点应该行得通$http.post
返回一个承诺,但它不是then
,而是成功
。那以前把我绊倒了。如果您返回该承诺,控制器中的代码应该正常工作
createCategory: function(name){
var params = 'categoryName' + '=' + name.categoryName + '&';
params += 'method' + '=' + 'createCategory';
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
return $http.post('cfc/category.cfc', params)
.success(function(data){
return data;
});
}
这阻止了错误的发生,但列表仍然没有更新。那么,您可能在服务器上遇到错误。我是否仍然必须使用控制器中的。然后?您可以使用。显然,HttpPromise既成功又成功。它正在工作,并且没有抛出错误,但是列表仍然没有通过返回得到更新。我已经将控制器代码更新为当前状态$scope.categoryNames
实际上是一个对象数组,因此我必须更新,必须将新项推入其中。then()
和success()
在$http.post()
的promise上都可用,但它们向回调传递不同的参数then()
传递一个响应对象,而success()
是从响应中获取数据并将其传递给回调的速记。最简单的方法是切换到success()
。