gpt4 book ai didi

javascript - 在 AngularJS 中创建收藏夹列表

转载 作者:行者123 更新时间:2023-11-28 18:21:02 27 4
gpt4 key购买 nike

我想根据用户选择在我的应用程序中创建一个收藏夹列表。在我的应用程序中,我使用一个长 JSON 文件,其中包含一堆使用 $http.get() 加载的文本。这是在我的 View 中显示内容的代码。

<ion-view>
<ion-nav-title></ion-nav-title>
<ion-content>
<div class="card" ng-repeat="list in items | filter: { id: whichid }">
<div class="item item-text-wrap"
ng-repeat="item in list.content | filter: { id2: whichid2 }"
ng-bind-html="item.description.join('')">
<h3>{{ item.name }}</h3>
<p>{{ item.description }}</p>
</div>
</div>
</ion-content>

创建收藏夹列表的基本思想是将显示的文本保存在数组中。之后,我可以轻松地在收藏夹列表的模板中打印该数组。

所以问题是如何将文本/数据表单表达式 ({{ item.name }}, {{ item.description }}) 保存到数组中?或者如果有人有其他想法来制作最喜欢的列表。

最佳答案

使用 ng-click 将项目详细信息传递到 Controller 中定义的函数,并将其插入数组,如下所示:

<ion-view>
<ion-nav-title></ion-nav-title>
<ion-content>
<div class="card" ng-repeat="list in items | filter: { id: whichid }">
<div class="item item-text-wrap" ng-click="favouriteThis(item)"
ng-repeat="item in list.content | filter: { id2: whichid2 }"
ng-bind-html="item.description.join('')">
<h3>{{ item.name }}</h3>
<p>{{ item.description }}</p>
</div>
</div>
</ion-content>

在您的 Controller 中:编写“favouriteThis”函数,以便在用户每次点击收藏项时推送它:

$scope.favouriteList = [];
$scope.favouriteThis = function (item) {
$scope.favouriteList.push(item); // make sure to check for duplicates before pushing the item, the logic for which i've not included here.
}

由于您在“$scope.favouriteList”中拥有所有收藏的项目详细信息,因此您可以直接在收藏列表中使用该信息。为了使其更准确,在检查重复项时,您还可以记录用户与特定项目交互的次数,使用该次数您可以在列表顶部显示交互次数最多的项目。希望这有帮助:)

关于javascript - 在 AngularJS 中创建收藏夹列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39878728/

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