gpt4 book ai didi

javascript - 有没有办法使用 Angular 在单击按钮时显示内容?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:31 25 4
gpt4 key购买 nike

我想在我的页面上显示文章列表。首先显示所有内容,但如果用户单击一个按钮,它只会显示与单击的按钮相关的文章。这意味着它会查看数组中的“葡​​萄”,如果它是红色的,则显示列表中的红酒。最好有一个按钮来显示所有默认返回初始 View 的内容。欢迎任何帮助。

<button>Show ALL Articles<button>
<button ng-click="filter('red')">Show me articles with red wines<button>
<button ng-click="filter('white')">Show me articles with white wines<button>
<button ng-click="filter('rose')">Show me articles with rose wines<button>

<ul>
<li ng-repeat="item in wineListings | filter: filter">
<h2>{{item.country}}</h2>
<p>{{item.grape}}</p>
</li>
</ul>
$scope.wineListings = [
{
country:"France",
grape:"red"
},
{
country:"Spain",
grape:"red"
},
{
country:"Italy",
grape:"white"
},
{
country:"USA",
grape:"rose"
}
];

最佳答案

您只需在您的作用域中添加一个作为参数传递给 | 的附加变量即可实现您的需求。通过 Angular 内置的对象匹配语法过滤,具体来说:

wineListings | filter: { grape: selected }

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

app.controller('testCtrl',function($scope) {
$scope.wineListings = [
{
country:"France",
grape:"red"
},
{
country:"Spain",
grape:"red"
},
{
country:"Italy",
grape:"white"
},
{
country:"USA",
grape:"rose"
}
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<button ng-click="selected=''">Show me all wines</button>
<button ng-click="selected='red'">Show me red wines</button>
<button ng-click="selected='white'">Show me white wines</button>
<button ng-click="selected='rose'">Show me rose wines</button>

<ul>
<li ng-repeat="item in wineListings | filter : { grape: selected }">
<h2>{{item.country}}</h2>
<p>{{item.grape}}</p>
</li>
</ul>
</body>

关于javascript - 有没有办法使用 Angular 在单击按钮时显示内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51338761/

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