我正在尝试使用提交按钮添加新数据和选择获胜者按钮来挑选新的获胜者。两个按钮都显示,但是当我点击它们时没有任何反应。
这是我的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
答案 0 :(得分: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)]