这是我的代码:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<link href="app.css" rel="stylesheet" type="text/css"/>
<title>TNF FANTASY</title>
</head>
<body>
<div class="app" ng-app="myApp" ng-controller="myCtrl">
<div class="total">
<i><h4>Money Remaining: <br>{{total |currency}}</h4></i> <h4>{{playersSelected}}</h4>
</div>
<ul>
<li class="players" ng-repeat="player in players">
{{player.name}} <br>
{{player.Team}} <br>
{{player.price| currency}} <br>
{{player.position| uppercase }}
<br>
<button ng-click="buy($index)">buy</button>
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.playersSelected = "0/6";
$scope.total = 50000000;
// keep track of what was bought already
$scope.history = [];
$scope.denteries = [];
$scope.players = [
{name : "Yasin 'YB' Amusan", Team : "Industry", price: 8000000, position: 'forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Hassan 'Hasi' Akinyera", Team : "Industry", price: 5000000, position: 'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Femi 'Fabio' Awoniyi", Team : "Industry", price: 9000000, position: 'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Deji 'Dej' Awoniyi", Team : "Industry", price: 7000000, position: 'forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Koye 'K10' Kekere-Ekun", Team : "Industry", price: 9000000, position:'midfielder', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Teni 'Teezee' Zacchaeus", Team : "Industry", price: 6000000, position:'hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Bolaji 'Boj' Odojukan", Team : "Industry", price: 7000000, position:'forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Ernest", Team : "Industry", price: 6000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png' },
{name : "Fikayo 'Kyo' Etti", Team : "Industry", price: 8000000, position:'Defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Efe Tunde-Imoyo", Team : "Industry", price: 5000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Temi 'Forbes' Afolabi", Team : "VGC", price: 14000000, position:'forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Dami Etomi", Team : "VGC", price: 7000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Tomi 'Belg' Belgore", Team : "VGC", price: 8000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Tobi 'Staxx' Kasali", Team : "VGC", price: 9000000, position:'Hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Bobo", Team : "VGC", price: 9000000, position:'Hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Ola Adeyemi", Team : "VGC", price: 5000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Pekun 'Pyzzle' Odutola", Team : "VGC", price: 9000000, position:'Hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Oreva 'Revz' Amata", Team : "VGC", price: 11000000, position:'forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Tenny 'TK' Karim", Team : "YMFC", price: 5000000, position:'Forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Andy Inegbese", Team : "YMFC", price: 5000000, position:'Midfielder', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Donald 'DO' Ofik", Team : "YMFC", price: 7000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Zachy Mbadiwe", Team : "YMFC", price: 10000000, position:'Forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Emeke 'Mexxo'", Team : "YMFC", price: 6000000, position:'midfielder', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Ife 'I-Baxx Bakare", Team : "YMFC", price: 6000000, position:'midfielder', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Gabriel 'Gabby' Inegbese", Team : "YMFC", price: 8000000, position:'Hybrid',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Jimi Oyelola", Team : "YMFC", price: 5000000, position:'Forward', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Chad 'Chadea' Oyefolu", Team : "ChadFC", price: 5000000, position:'Forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Ekoh 'Starboy' Sagoe", Team : "ChadFC", price: 15000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Tomiwa 'JBET' Jaiyeola", Team : "ChadFC", price: 13000000, position:'Forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Lemar Chris", Team : "ChadFC", price: 9000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Olumide 'Olic' Williams", Team : "ChadFC", price: 7000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Leke 'LVD' Dokomu", Team : "ChadFC", price: 7000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Dola Awosika", Team : "ChadFC", price: 5000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Nehemiah 'Memphis'", Team : "ChadFC", price: 7000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Kola 'Skippo' Ayanwale", Team : "ChadFC", price: 10000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Milekan 'Mileks'", Team : "ChadFC", price: 7000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Lanre 'Lavigz' Vigo", Team : "ChadFC", price: 8000000, position:'hybrid', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Alfred 'Flyz' Obande", Team : "FBGFC", price: 6000000, position:'defender', image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Chuka 'Zingy' Azinge", Team : "FBGFC", price: 6000000, position:'hybrid',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Osobase 'Oso' OmoKhodion", Team : "FBGFC", price: 10000000, position:'midfielder',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Arnold Okuguni", Team : "FBGFC", price: 7000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Ikenna Mbadiwe", Team : "FBGFC", price: 6000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Moukhtar", Team : "FBGFC", price: 8000000, position:'forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Chinedu 'Anuxx' Anukwem", Team : "FBGFC", price: 8000000, position:'forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Tosan 'Mac' Wiltshere", Team : "FBGFC", price: 6000000, position:'defender',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'},
{name : "Martin Agbaso", Team : "FBGFC", price: 5000000, position:'forward',image: src='http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png'}
];
$scope.buy = function(i) {
var p = $scope.history.indexOf(i);
if (p !== -1) { //if bought, cancel it
$scope.history.splice(p, 1);
$scope.total += $scope.players[i].price;
$scope.playersSelected = $scope.history.length + "/6";
} else { //else add it to history
if ($scope.total < $scope.players[i].price) {
return;
}
$scope.history.push(i);
$scope.total -= $scope.players[i].price;
$scope.playersSelected = $scope.history.length + "/6";
}
};
});
</script>
</body>
</html>
我试图让每个用户只能买两个位置为“后卫”或“混合”的玩家,两个位置为“中场”或“混合”的玩家,以及两个位置为“前锋”的玩家或者“混合”。这使得总共有6名玩家可以购买。
我尝试过使用indexOf,但这只会显示历史数组中所选玩家的位置,而不会显示购买上述每个位置的玩家的次数。
这可以通过Angular JS完成,还是过于雄心勃勃?
答案 0 :(得分:0)
您可以为此目的编写辅助函数:
function getOccurrences(array, item) {
var occurrences = 0;
for (var index in array) {
if (array[index] === item) {
occurrences++;
}
}
return occurrences;
}
编辑:
根据评论部分写的内容,我意识到我提供的代码还不够。所以,让我们将功能更改为:
function getOccurrences(players, array, item, helper) {
var occurrences = 0;
for (var index in array) {
if (((typeof helper === "function") && (helper(players, array[index], item))) || ((typeof helper !== "function") && (array[index] === item))) {
occurrences++;
}
}
return occurrences;
}
我们需要一个帮助器,如下所示:
function myHelper(players, val, item) {
return players[val] && (players[val].position === item);
}
然后像这样调用它:
getOccurrences($scope.players, $scope.history, "defender", myHelper);