在角度js webapp中显示分数

时间:2017-03-18 16:07:53

标签: javascript html angularjs windows web-applications

每次选择一个玩家时,如何让显示的分数中的分子上升一个?

其次,如果超过50,000美元的限额或选择了6名玩家,我如何阻止用户选择玩家。

我尝试将“0/6”中的一小部分作为字符串放到“6/6”但似乎效率低下。帮助

这是索引的链接:

https://www.w3schools.com/code/tryit.asp?filename=FDRFSHK2WONU

2 个答案:

答案 0 :(得分:0)

1)将{{playersSelected}}替换为{{history.length}}/6

2)在您的购买功能下,将其添加到历史记录中,检查您是否已经拥有6并且价格不超过总数。

$scope.buy = function(i) {

  var p;

  // check if already bought
  if ((p = $scope.history.indexOf(i)) !== -1) {

    // already bought so cancel it
    $scope.history.splice(p, 1);
    $scope.total += $scope.players[i].price;

  } else {
   alert($scope.history.length);
    // check we do not have 6 already
    if ($scope.history && $scope.history.length > 5) {
      return;
    }

    // check the price does not exceed the total left over
    if ($scope.players[i].price > $scope.total) {
      return;
    }

    // add to history
    $scope.history.push(i);
    $scope.total -= $scope.players[i].price;

  }

答案 1 :(得分:0)

只需更改“$ scope.buy”功能,根据需要修改“$ scope.playersSelected”。

    (function() {
        'use strict';

        angular.module('carApp').factory('HomeResourceService',
            HomeResourceService);

        HomeResourceService.$inject = [ '$http' ];

        function HomeResourceService($http) {
            var service = {};

            var getTestDataUrl = '/api/data';

            service.getTestData = function(key, value) {
                return $http({
                    method : "GET",
                    url : getTestDataUrl,
                    params : {
                        operator : key,
                        market : value
                    }
                });
            };

            return service;


}
})();
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.playersSelected = "0/6";
  $scope.total = 50000000;
  //keep trak of what was bought already
  $scope.history = [];
  $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 bought, cancel it
    if (p !== -1) {
      $scope.history.splice(p, 1);
      $scope.total += $scope.players[i].price;
    } 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";
    }
  };
});