【问题标题】:Any way to go through list? Ionic Framework有什么办法可以通过列表吗?离子框架
【发布时间】:2017-02-28 11:55:30
【问题描述】:

您好,我有物品清单。像这样:

// Some fake testing data
  var chats = [{
    id: 0,
    name: 'Ben Sparrow',
    lastText: 'London',
    face: 'img/ben.png'
  }, {
    id: 1,
    name: 'Max Lynx',
    lastText: 'Paris',
    face: 'img/max.png'
  }, {
    id: 2,
    name: 'Adam Bradleyson',
    lastText: 'Berlin',
    face: 'img/adam.jpg'
  }, {
    id: 3,
    name: 'Perry Governor',
    lastText: 'Tokio',
    face: 'img/perry.png'
  }, {
    id: 4,
    name: 'Mike Harrington',
    lastText: 'Milan',
    face: 'img/mike.png'
  }];

如您所见,每个列表项都有一个名为 city 的 lastText。这是我的视图文件: `

  <ion-item class="item-remove-animate item-avatar item-icon-right" ng-init="getWeather(chat.lastText)" ng-repeat="chat in chats" type="item-text-wrap" href="#/tab/chats/{{chat.id}}" >
    <img ng-src="{{chat.face}}">

    <h2>{{cityname}}</h2>
    <p>{{dweather}}</p>
    <i class="icon ion-chevron-right icon-accessory"></i>

    <ion-option-button class="button-assertive" ng-click="remove(chat)">
      Delete
    </ion-option-button>
  </ion-item>
</ion-list>

`

我有一个函数getWeather(chat.lastText),它返回所选城市的天气。变量citynamedweather。我想显示存储在每个列表项的 lastText 变量中的每个城市的天气。但它只显示每个列表项的第一个城市的天气。我的问题是如何使它成为可能?

【问题讨论】:

标签: javascript angularjs ionic-framework


【解决方案1】:

我不知道您的 getWeather 函数是什么样的,但不要将结果分配给“全局”范围变量,而应该将它们放入 chat 对象:

$scope.getWeather = function(chat) {
    // get weather
    weatherService.getWeather(chat.lastText).then(function(response) {
        var weatherResult = response.data;
        chat.cityname = weatherResult.cityname;
        chat.dweather = weatherResult.dweather;
    });
}

那么你可以这样显示:

<ion-item ng-init="getWeather(chat)">
    {{ chat.cityname }}
    {{ chat.dweather }}
</ion-item>

【讨论】:

  • 那么请在您的问题中包含您的getWeather 函数的外观。就像我说的,因为你没有包含你的代码,所以我做了一些假设,当然你必须根据它的编写方式将这些假设应用于你自己的代码
【解决方案2】:

ng-init 中删除getWeather(chat.lastText) 并尝试以下方式。 ng-initng-repeat 中只调用一次。

<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="chat in chats" type="item-text-wrap" href="#/tab/chats/{{chat.id}}">
  <img ng-src="{{chat.face}}">
{{getWeather(chat.lastText)}}
  <h2>{{cityname}}</h2>
  <p>{{dweather}}</p>
  <i class="icon ion-chevron-right icon-accessory"></i>

  <ion-option-button class="button-assertive" ng-click="remove(chat)">
    Delete
  </ion-option-button>
</ion-item>
</ion-list>

或尝试以下方式使用另一个 ng-repeat 仅用于调用函数

<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="chat in chats" type="item-text-wrap" href="#/tab/chats/{{chat.id}}">
  <img ng-src="{{chat.face}}">
  <div ng-repeat="n in getWeather(chat.lastText)"></div>
  <h2>{{cityname}}</h2>
  <p>{{dweather}}</p>
  <i class="icon ion-chevron-right icon-accessory"></i>

  <ion-option-button class="button-assertive" ng-click="remove(chat)">
    Delete
  </ion-option-button>
</ion-item>
</ion-list>

【讨论】:

  • 我以前试过这个。它会导致硬循环。列表中的所有项目都会快速通过列表视图而不会停止。它会导致循环
猜你喜欢
  • 1970-01-01
  • 2023-02-06
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 2019-02-04
相关资源
最近更新 更多