抱歉,如果这听起来很蠢。
我遇到了类似于 this question 的问题虽然接受的答案有效,但它也带来了另一个问题:当我向数组添加一个新对象时,Angular 不会呈现它。
app.controller('MainCtrl', [
'$scope',
'$filter',
'posts',
function($scope, $filter, posts) {
$scope.posts = $filter('orderBy')(posts.posts, '-votes')
}
]
我的添加函数:
o.addPost = function(post) {
return $http.post('/posts', post).success(function(data) {
o.posts.push(data)
})
}
我能做些什么吗?
编辑:我要这样做:
o.addPost = function(post) {
return $http.post('/posts', post)
}
app.controller('MainCtrl', [
'$scope',
'$filter',
'posts',
function($scope, $filter, posts) {
$scope.posts = $filter('orderBy')(posts.posts, '-votes')
$scope.addPost = function() {
posts.addPost(param).then(function(response) {
$scope.posts.push(response.data)
})
}
]
最佳答案
在工厂中只返回 http 而不是在工厂内展开 promise
o.addPost = function(post) {
return $http.post('/posts', post);
}
然后在工厂内部调用addPost
方法,并在promise中捕获promise。
app.controller('MainCtrl', [
'$scope',
'$filter',
'posts',
function($scope, $filter, posts) {
var params = {};
posts.addPost(params).then(function(data) {
$scope.posts = $filter('orderBy')(data, '-votes')
})
}
])
关于javascript - AngularJS orderBy 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43860590/