對不起,如果這聽起來很愚蠢。AngularJS orderBy問題
我遇到類似於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)
})
}
]
好主意。儘管存在小問題,但您必須將promise中的'data.data'推入'$ scope.posts'中。我希望過濾器只在視圖初始化時才起作用。 –