角度点击不点火

时间:2015-11-04 21:10:13

标签: javascript angularjs

我不明白我犯了什么错误。

<div ng-app="nameApp" ng-controller="nameCntrl">
    <table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">
      <thead><tr><th>Name</th><th>Value</th></tr></thead><tbody>  
        <tr><td>Chris </td> 
            <td><input type="text" size="4" ng-model="numDays"/>&nbsp;
                <input type="button" value="submit"  ng-click="submitbutton"/></td></tr>


    </table>
        {{numDays}}
</div>

var app = angular.module('nameApp', []);
app.controller('nameCntrl',function($scope) {
    console.log($scope);
    $scope.numDays = 5;

    $scope.submitbutton = function() {
        alert($scope.numDays);
    }
});

http://jsfiddle.net/srvdfv6y/1/

2 个答案:

答案 0 :(得分:3)

您编写的应用程序是正确的,只要注意在事件 DOM就绪之前加载角度脚本。

参见 updated fiddle

另外,正如@TjGienger所注意到的,ng-click="submitbutton"应为ng-click="submitbutton()"

答案 1 :(得分:1)

您需要将submitbutton()指定为ng-click声明

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
  var app = angular.module('nameApp', []);
  app.controller('nameCntrl', function($scope) {
    console.log($scope);
    $scope.numcDays = 5;
    $scope.myValue = true;


    $scope.submitbutton = function() {
      alert($scope.numcDays);
      $scope.myValue = false;
    }
  });
</script>

<body>
  <div ng-app="nameApp" ng-controller="nameCntrl">
    <table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">
      <thead>
        <tr>
          <th>Name</th>
          <th>Value</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Chris</td>
          <td>
            <input type="text" size="4" ng-model="numDays" />
            <input type="button" value="submit" ng-click="submitbutton()" />
          </td>
        </tr>
    </table>{{numDays}}
    <div ng-hide="myValue">Answer submitted</div>
  </div>
</body>
&#13;
&#13;
&#13;