[英]How can i use array of objects from database as filter?
// filter with data from database not working app.filter('position', function($http, dbOperations) { console.log(dbOperations.getAccessPosition()); var positions = []; //[{name:"cashier",id:1},{name:"operator",id:2}]; // get the object array from database with name and id dbOperations.views("getPositions", "").then(function(res) { positions = res; // this is the desired value: [{name:"cashier",id:1},{name:"operator",id:2}] }); var poitionName = ""; return function(positionNum) { positions.forEach(function(p) { if (p.id == positionNum) { poitionName = p.name; return false; } }); return poitionName; } }); app.service('dbOperations', function($http) { this.getAccessPosition = function() { return $http({ method: "POST", url: "/common/functions.php", data: { 'process': "getAccessPosition", 'data': "" } }).then(function success(res) { return res; }, function myError(response) { // console.log("Error"); }); } });
When I console.log the positions, it prints the data that I need. 当我console.log位置时,它会打印我需要的数据。 but the filter is not working. 但过滤器不起作用。 maybe because the data is from database and it is waiting to respond. 可能是因为数据来自数据库并且正在等待响应。 dbOperations is the in the service and I use $http. dbOperations是服务中的,我使用$ http。 Please help me with this. 请帮我解决一下这个。 Thankyou. 谢谢。
In the service, just return the http request instead of unwrapping the promise. 在服务中,只需返回http请求,而不是解开promise。
app.service('dbOperations', function($http) {
this.getAccessPosition = function() {
return $http({
method: "POST",
url: "/common/functions.php",
data: {
'process': "getAccessPosition",
'data': ""
}
})
}
});
in the filter do the service call inside the callback function. 在过滤器中,在回调函数中进行服务调用。
app.filter('position', function($http, dbOperations) {
console.log(dbOperations.getAccessPosition());
var positions = []; //[{name:"cashier",id:1},{name:"operator",id:2}];
var poitionName = "";
return function(positionNum) {
dbOperations.views("getPositions", "").then(function(res) {
positions = res.data;
positions.forEach(function(p) {
if (p.id == positionNum) {
poitionName = p.name;
return false;
}
});
return poitionName;
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.