search

Home  >  Q&A  >  body text

angular.js - angularjs factory returns an array and the front ionic does not display

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.

巴扎黑巴扎黑2864 days ago573

reply all(1)I'll reply

  • 滿天的星座

    滿天的星座2017-05-15 17:00:30

    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;
    }

    reply
    0
  • Cancelreply