Javascript AngularJS自定义过滤器在ng repeat中不';不要被推到物体上
我在angular中的自定义过滤器函数有问题 这是过滤器功能:Javascript AngularJS自定义过滤器在ng repeat中不';不要被推到物体上,javascript,angularjs,filter,angularjs-ng-repeat,ng-repeat,Javascript,Angularjs,Filter,Angularjs Ng Repeat,Ng Repeat,我在angular中的自定义过滤器函数有问题 这是过滤器功能: .filter('itemFilter', function($rootScope) { return function(input) { var filtered = []; angular.forEach(input, function(item) { if(item.user_id === $rootScope.userData.
.filter('itemFilter', function($rootScope) {
return function(input) {
var filtered = [];
angular.forEach(input, function(item) {
if(item.user_id === $rootScope.userData._id && item.active === true){
filtered.push(item);
}
});
//console.log(filtered);
return filtered;
}
});
筛选器执行它应该执行的操作,但是当我想在item数组中推送一个新对象时,筛选器不会获取新对象。不过,新项被正确地推送到数组中
这是在DB和rootScope中推送项的位置。该函数位于工厂内部,在控制器中调用
工厂中的功能:
var addItem = function(item){
$http.post("http://localhost:3333/items", item)
.success(function (response) {
$rootScope.items.push(item);
$log.debug(response);
})
.error(function (err){
$log.error("item was not added: "+err);
});
};
在控制器中调用函数:
$scope.itemObj = {"user_id": $rootScope.userData._id, "group_id": $rootScope.userData.group_id};
$scope.addItem = function() {
console.log($scope.itemObj);
dataFactory.addItem($scope.itemObj);
$modalInstance.close();
$scope.itemObj = {"user_id": $rootScope.userData._id, "group_id": $rootScope.userData.group_id};
};
包含所有项的整个数组位于根范围中
这是我称之为ng repeat的部分:
<tbody ng-repeat="item in items | itemFilter | filter: qry | orderBy: '-created'">
<tr popover="{{item.description}}" popover-trigger="mouseenter">
<td>{{item.amount}}</td>
<td>{{item.name}}</td>
<td>{{item.price | currency:"EUR€ "}}</td>
<td>{{item.list}}</td>
<td ng-controller="EditItemCtrl"><a class="btn btn-default" ng-click="open(item)">Edit</a></td>
<td><a class="btn btn-danger" ng-click="deleteItem(item)">Delete</a></td>
<td ng-controller="BoughtItemCtrl">
<a class="btn btn-success" ng-click="open(item)">Bought</a>
</td>
<td>
<p ng-show="!item.active">NO</p>
<p ng-show="item.active">YES</p>
</td>
</tr></tbody>
{{item.amount}}
{{item.name}
{{项目价格|货币:“欧元”}
{{item.list}
编辑
删除
买了
否
是
有没有办法告诉过滤器,一个新项目已添加到数组中?重新加载页面后,一切正常,因为过滤器会获取整个项目数组。我想我已经解决了这个问题 我现在将“响应”推到数组中,而不是将“项”推到数组中
var addItem = function(item){
$http.post("http://localhost:3333/items", item)
.success(function (response) {
$rootScope.items.push(response);
$log.debug(response);
})
.error(function (err){
$log.error("item was not added: "+err);
});
};
推送响应现在有一个“_id”值(由MonogDB生成)。我不确定丢失的身份证是否是问题所在
也许你有答案。
无论如何,感谢您的快速回复。我想我已经解决了这个问题 我现在将“响应”推到数组中,而不是将“项”推到数组中
var addItem = function(item){
$http.post("http://localhost:3333/items", item)
.success(function (response) {
$rootScope.items.push(response);
$log.debug(response);
})
.error(function (err){
$log.error("item was not added: "+err);
});
};
推送响应现在有一个“_id”值(由MonogDB生成)。我不确定丢失的身份证是否是问题所在
也许你有答案。
无论如何,感谢您的快速响应。请显示控制器。您是如何定义“项”的?如果您提供了一个工作项,这会有所帮助。请删除
$scope.$apply(function())
,只需按一下即可。通过$http.get在.run函数中初始化“items”数组。因此,重新加载后,该数组将填充来自DB@dfsq我以前试过。也不起作用:/请显示控制器。您是如何定义“项”的?如果您提供一个有效的。删除$scope.$apply(函数())
,只需按一下即可。通过$http.get在.run函数中初始化“items”数组。因此,重新加载后,该数组将填充来自DB@dfsq我以前试过。也不起作用:/