javascript - AngularJS call function of object in ng-repeat - Stack Overflow

I have the following controller:app.controller('TestController', function($scope) {$scope.tes

I have the following controller:

app.controller('TestController', function($scope) {
  $scope.tests = [{
      name: 'Name 1',
      description: 'Some Description',
      actionHandler: function() {
        alert('Action Handler called');
      }
    },
    ...
  ];
});

In my htmlfile I do an ng-repeat like this:

<div ng-repeat="test in tests">
  <p>{{test.name}}</p>
  <p>{{test.description}}</p>
  <a ng-click="{{test.actionHandler}}">Click me</a>
</div>

It is not really working. I also tried.

<a ng-click="test.actionHandler">Click me</a>

And

 <a ng-click="test.actionHandler()">Click me</a>

But none seem to work. Any idea how I can call a function of an object inside ng-repeat?

Thanks, xCoder.

I have the following controller:

app.controller('TestController', function($scope) {
  $scope.tests = [{
      name: 'Name 1',
      description: 'Some Description',
      actionHandler: function() {
        alert('Action Handler called');
      }
    },
    ...
  ];
});

In my htmlfile I do an ng-repeat like this:

<div ng-repeat="test in tests">
  <p>{{test.name}}</p>
  <p>{{test.description}}</p>
  <a ng-click="{{test.actionHandler}}">Click me</a>
</div>

It is not really working. I also tried.

<a ng-click="test.actionHandler">Click me</a>

And

 <a ng-click="test.actionHandler()">Click me</a>

But none seem to work. Any idea how I can call a function of an object inside ng-repeat?

Thanks, xCoder.

Share Improve this question edited Nov 17, 2017 at 4:14 Danny Fardy Jhonston Bermúdez 5,5715 gold badges26 silver badges38 bronze badges asked Oct 7, 2015 at 14:37 Janosch HübnerJanosch Hübner 1,6941 gold badge28 silver badges49 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

Your third stab is the correct form test.actionHandler(). Perhaps you could try including $window along side $scope in your controller 'TestContorller', function($scope, $window) and change the alert call to $window.alert('action handler called') ...

Just remove the {{}} and invoke the function within ng-click

<a ng-click="test.actionHandler()">Click me</a>

DEMO

If I were you, I will do it like this

For html:

<div ng-repeat="test in tests">
   <p ng-bind="test.name"></p>
   <p ng-bind="test.description"></p>
   <a ng-click="test.actionHandler($index)">Click me</a>
</div>

For Controller

app.controller('TestController',function($scope) {
   $scope.tests = [
      {
         name: 'Name 1',
         description: 'Some Description',
      },
   ];

   scope.actionHandler = function(index) 
   {
      alert('Action Handler called for'+$scope.tests[index]['name']);
   }

});

Use ng-bind instead of the curly braces. That is my tip to you.

I would create a new event handler in the controller:

app.controller('TestController',function($scope) {

   $scope.handleClick = function(){
       alert('Action Handler called');   
   }

});

And then your link would be:

<a ng-click="handleClick()">Click me</a>

If you need a handler for a specific test then just pass an id (for example) to the handler.

Only remove the curly braces and add parentheses ().

ng-click="test.actionHandler()"

Something like this:

(function() {
  angular.module("myApp", []).controller("Controller", ["$scope",
    function($scope) {
      $scope.tests = [{
        name: "Name 1",
        description: "Some Description...",
        actionHandler: function() {
          alert("Action Handler called");
          console.log(this);
        }
      }, {
        name: "Name 2",
        description: "Some Description 2...",
        actionHandler: function() {
          alert("Action Handler called");
          console.log(this);
        }
      }];
    }
  ]);
})();
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<div data-ng-app="myApp">
  <div data-ng-controller="Controller">
    <ul>
      <li data-ng-repeat="test in tests">{{test.name}}
        <br />
        <button data-ng-click="test.actionHandler()" type="button">Test</button>
      </li>
    </ul>
  </div>
</div>

Live demo

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745039894a4607747.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信