0

I have consulted these related articles, but the presented solutions do not work:

Here is my 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"
    }
  ]
});
Community
  • 1
  • 1
user1429980
  • 6,872
  • 2
  • 43
  • 53
  • `ng-click` isn't like inline js. InlineJS is awful. You need to do `$scope.alert = alert.bind(window)` in order for it to be available in your view. However, you should also not directly access globals like that. It would be ideal to inject `$window` into the controller and do `$scope.alert = $window.alert.bind($window);` – m59 Mar 16 '15 at 22:38

1 Answers1

4

It does work, but alert is not part of your $scope so it won't do anything.

You can add it in your controller:

$scope.alert = function(message) { alert(message); }
floribon
  • 19,175
  • 5
  • 54
  • 66