suchen

Heim  >  Fragen und Antworten  >  Hauptteil

angular.js - angularjs factory 返回数组前台ionic不显示

刚接触ionic和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];
    }
  };
},

};
})

"//标注"的地方这样是正常的,但是改成return recos[0]或者recos[1]...前台调用就不显示数据,这是为什么呢~;因为想做一个简单的匹配功能:

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

};

碰到了这个问题,不知道其中原因。

巴扎黑巴扎黑2744 Tage vor542

Antworte allen(1)Ich werde antworten

  • 滿天的星座

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

    allreco的意思应该是返回所有的reco,为什么会变成return recos[i],你是想返回所有在localStorage.city里面的reco吗?

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

    Antwort
    0
  • StornierenAntwort