简体   繁体   English

如何使用数据库中的对象数组作为过滤器?

[英]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.

相关问题 我可以使用过滤器从对象数组中提取值吗? - Can I use filter to extract values from an array of objects? 如何使用另一个对象中的值过滤对象数组? - How can I filter an array of objects with a value from another object? 如何仅从对象数组中过滤特定数据 - How can I filter only a particular data from an array of objects 如何过滤这个对象数组? - How can I filter this array of objects? 如何从我从 React 中的另一个数组对象创建的数组中进行过滤? - How can I filter from an Array that i created from another array objects in React? 如果过滤键可以有任何类型的值,我如何过滤数组中的唯一对象? - How do I filter the unique objects from an array if the filter key can have any type of value? 如何从按一个值过滤的对象数组创建一个新数组? - How can I create a new array from an array of objects filter by one value? 如何排序 / map / 过滤以下 javascript 对象数组从一种格式到另一种格式 - How can I sort / map / filter the following javascript array of objects from one format to another 如何比较Javascript日期以过滤数组中的对象? - How can I compare Javascript dates to filter objects in an array? 如何使用jmesPath过滤具有数组的表达式的javascript对象? - How can I filter javascript objects with an expression that has array in it with jmesPath?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM