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"
    }
  ]
});
1
  • 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, 2015 at 22:38

1 Answer 1

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); }
4
  • even though alert is a global function ?
    – vsync
    Mar 16, 2015 at 22:39
  • How would you call an object function? ng-click="myObject.method(params)? It currently does nothing? Mar 16, 2015 at 22:45
  • Yes, what you access in the HTML are all the properties of the $scope object.
    – floribon
    Mar 16, 2015 at 22:45
  • ng-click="myObject.method(params) will work if $scope.myObject exists and has a method function
    – floribon
    Mar 16, 2015 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.