点击什么都不做

时间:2015-03-16 22:35:50

标签: javascript angularjs

我已经查阅了这些相关文章,但所提出的解决方案不起作用:

这是我的plunkr :: http://plnkr.co/edit/kha6mAKDBtrY0XtTc6Wp?p=preview

<body ng-controller="MainCtrl">
<table>
  <caption>Troubles with ng-repeat and ng-click</caption>
  <thead>
    <tr>
      <th>Name</th>
      <th>Occupation</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="employee in employees" ng-click="alert('hi')">
      <td>{{employee.name}}</td>
      <td>{{employee.occupation}}</td>
    </tr>
  </tbody>
</table>

var app = angular.module("plunker", []);

app.controller("MainCtrl", function($scope) {
  $scope.employees = [
    {
      name: "John Doe",
      occupation: "Miner"
    },
    {
      name: "Theressa",
      occupation: "Construction Worker"
    }
  ]
});

1 个答案:

答案 0 :(得分:4)

它确实有效,但alert不属于您的$scope,所以它不会做任何事情。

您可以在控制器中添加它:

$scope.alert = function(message) { alert(message); }