0

我正在尝试使用提交按钮添加新数据和选择获胜者按钮来选择新获胜者。两个按钮都显示,但是当我点击它们时没有任何反应。

这是我的 index.html 文件。

都是关于狗的!

<div ng-controller="DogCtrl">
<form ng-submit="addDog()">
<input type="text" ng-model="newDog.name">
<input type="submit" value="Add">
</form>

    <ul>
    <li ng-repeat="dog in dogs">
    {{dog.name}}
    <span ng-show="dog.winner" class="winner">WINNER</span>
</li>
</ul>

<button ng-click="drawWinner()">Draw Winner</button>
</div>

My js.coffee file.

@DogCtrl = ($scope) ->
   $scope.dogs = [
 {name: "Babytheboxer"}
 {name: "RaleightheJack"}
 {name: "Frankie"}
  ]

$scope.addDog = ->
  $scope.dogs.push($scope.newDog)
  $scope.newDog = {}

$scope.drawWinner = ->
  dog = $scope.dogs[Math.floor(Math.random()*$scope.dogs)]
  dog.winner = true
4

1 回答 1

0

我看到的唯一问题是在你的这一行js.coffee

dog = $scope.dogs[Math.floor(Math.random()*$scope.dogs)] 

这应该给你一些 javascript 错误,因为$scope.dogs它是一个数组。我假设你想要这里的数组长度。

所以试试:

dog = $scope.dogs[Math.floor(Math.random()*$scope.dogs.length)]
于 2013-08-16T04:29:42.930 回答