您好,我正在尝试在我的中继器中获得最低价格。我将在此列表项附近添加一个绿色标志。 (正如您在代码中看到的,“span class min-value”)
如何获得这个最小值并在 html 中使用我的跨度作为“ng-show”?
谢谢。
var app = angular.module("App", []);
app.controller("Controller", function($scope) {
$scope.priceList = [{
"name": "Product 1",
"price": "100"
}, {
"name": "Product 2",
"price": "200"
}, {
"name": "Product 3",
"price": "300"
}, {
"name": "Product 4",
"price": "400"
}, {
"name": "Product 5",
"price": "500"
}];
});
li {
padding: 10px;
}
.min-price {
width: 10px;
height: 10px;
background: green;
display: inline-block;
border-radius: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body ng-controller="Controller">
<ul>
<li ng-repeat="item in priceList">
{{item.name}} - {{item.price}}$
<span class="min-price" ng-show="true"></span>
</li>
</ul>
</body>
</html>
最佳答案
这是给您的简单逻辑:-
<span class="min-price" ng-show="isMin(item.price)"></span>
$scope.isMin=function(price){
var min=$scope.priceList[0].price;
for(var i=0;i<= $scope.priceList.length-1;i++){
if(Number($scope.priceList[i].price) < min){
min=$scope.priceList[i].price;
console.log("Min"+min);
}
}
console.log("min:"+min+ " price:"+price);
if(min===price){
return true;
}else{
return false;
}
}
关于angularjs - 如何找到 ng-repeat 中的最小值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29362082/