angular.js - angularjs factory returns an array and the front ionic does not display
巴扎黑
巴扎黑 2017-05-15 16:58:30
0
1
567

New to ionic and angular:
.factory('Recos', function() {

var recos=
[
    {   
        recoid:0,
        cityname:"上海",
        cityintroduce:"",
        citytext:"",
        img:"",
    },
    {
        recoid:1,
        cityname:"北京",
        cityintroduce:"",
        citytext:"",
        img:"",
    },
    {
        recoid:2,
        cityname:"深圳",
        cityintroduce:"",
        citytext:"",
        img:"",
    },
    {
        recoid:3,
        cityname:"",
        cityintroduce:"",
        citytext:"”。",
        img:"",
    }
];

return {
allreco: function() {
        return recos; //标注
},
get: function(recoid) {
  for (var i = 0; i < recos.length; i++) {
    if (recos[i].recoid === parseInt(recoid)) {
      return recos[i];
    }
  };
},

};
})

It is normal for the "//mark" to be like this, but change it to return recos[0] or recos[1]... The data will not be displayed when called in the front desk. Why is this~; Because I want to make a simple Matching function:

return {

allreco: function() {
  var city = localStorage.city;  
  for (var i = 0; i < recos.length; i++) {
      if (city.indexOf(recos[i].cityname) != -1){
        return recos[i];
      }
  };
},
get: function(recoid) {
  for (var i = 0; i < recos.length; i++) {
    if (recos[i].recoid === parseInt(recoid)) {
      return recos[i];
    }
  };
},

};

I encountered this problem and I don’t know the reason.

巴扎黑
巴扎黑

reply all(1)
滿天的星座

allreco should mean returning all recos. Why does it become return recos[i]? Do you want to return all recos in localStorage.city?

allreco: function(){
    var city = localStorage.city;  
    var result=[]
      for (var i = 0; i < recos.length; i++) {
          if (city.indexOf(recos[i].cityname) != -1){
           result.push(recos[i]);
          }
      };
    return result;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template