AngularJS提交按钮

时间:2013-08-16 03:36:06

标签: ruby-on-rails angularjs

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

这是我的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

1 个答案:

答案 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)]