搜尋

首頁  >  問答  >  主體

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 天前548

全部回覆(1)我來回復

  • 滿天的星座

    滿天的星座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;
    }

    回覆
    0
  • 取消回覆