gpt4 book ai didi

javascript - 如何使用 AngularJS 过滤对象数组?

转载 作者:太空宇宙 更新时间:2023-11-04 15:55:49 25 4
gpt4 key购买 nike

背景

上周我一直在自学 AngularJS。我创建了一个基本应用程序,用于获取每个 Twitch channel 的 Twitch 数据,并创建一个包含以下属性的“ channel ”对象数组;名称、 Logo url、流媒体(是或否)和 channel url。到目前为止,我已经使用 Angular 通过 ng-repeat 指令将每个 channel 对象的属性打印到表中(喜欢这个功能)。

问题/疑问

每当用户单击在线和离线按钮时,我希望能够对表中的对象列表进行排序,从而通过其“流”属性过滤 channel 。不幸的是,我似乎无法弄清楚如何按其属性过滤 channel 。有人可以帮忙吗?

HTML

  <div ng-controller="MainController" >
<div class="container" id="test">
<div class="row">
<header>
<ul id="mainMenu">
<li class="activeMenu" ng-click="filterGo('streaming')">All</li>
<li ng-click="filterGo('streaming')">Online</li>
<li ng-click="filterGo('streaming')">Offline</li>
</ul>
</header>
<div class="center-block" >
<table class="table table-hover" >
<thead>
<tr>
<th class="text-center">Logo</th>
<th class="text-center">Channel</th>
<th class="text-center">Online</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="channel in twitchChannels | filter:filtered" ng-click="setSelected();">
<td ><img id="size" ng-src="{{ channel.logo }}"></td>
<td >{{ channel.user }}</td>
<td >{{ channel.streaming }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

JS

var arr = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "habathcx", "RobotCaleb", "noobs2ninjas"];
var channels = [];
var count = 0;
var online = "";

// channel object
function Channel(user, name, logo, streaming) {
this.user = user;
this.name = name;
this.logo = logo;
this.streaming = streaming;
this.url = "https://www.twitch.tv/" + this.name;
}

var app = angular.module('myApp', []);


app.controller('MainController', function($scope, $http, $window) {
// loops through array of channels
for (var i = 0; i < arr.length; i++) {

(function(i) {

$http.get("https://wind-bow.gomix.me/twitch-api/users/" + arr[i]).success(function(data) {

var channel = data.display_name;
var name = data.name;
var logo = data.logo;

$http.get("https://wind-bow.gomix.me/twitch-api/streams/" + arr[i]).success(function(json) {
var streaming = (json.stream === null) ? false : true;
if(streaming) {
online = "YES";
channels[count] = new Channel(channel, name, logo, online);
} else {
online = "NO";
channels[count] = new Channel(channel, name, logo, online);
}
count++;
});
});
})(i);
}

$scope.twitchChannels = channels;

// if channel row is clicked take user to url
$scope.setSelected = function() {
$scope.selected = this.channel.url;
$window.open($scope.selected);
};

// Need help here!
$scope.filterGo = function(x) {
$scope.filtered = x;
}

});

这里有一个引用链接:http://codepen.io/bryanpleblanc/pen/jBBmgN

最佳答案

将您的 filterGo 函数更改为此

$scope.filterGo = function(x) {
$scope.filtered = channels.filter(c => c.streaming === x);
}

和您的ng-click

<li ng-click="filterGo('YES')">Online</li>
<li ng-click="filterGo('NO')">Offline</li>

注意

您最好使用 bool 值而不是 YESNO。这会更容易。

像这样

$http.get("https://wind-bow.gomix.me/twitch-api/streams/" + arr[i]).success(function(json) {
channels[count] = new Channel(channel, name, logo, json.stream !== null);
count++;
});

你的按钮

<li ng-click="filterGo(true)">Online</li>
<li ng-click="filterGo(false)">Offline</li>

还有过滤器

$scope.filterGo = function(x) {
$scope.filtered = channels.filter(c => c.streaming);
}

关于javascript - 如何使用 AngularJS 过滤对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42713637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com