简体   繁体   English

ng-repeat中对象的AngularJS调用函数

[英]AngularJS call function of object in ng-repeat

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 file I do an ng-repeat like this: 在我的文件中,我像这样执行ng-repeat

<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 ? 知道如何在ng-repeat调用对象的函数吗?

Thanks, xCoder. 谢谢,xCoder。

Your third stab is the correct form test.actionHandler() . 第三个刺是正确的形式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') ... 也许您可以尝试在控制器'TestContorller', function($scope, $window)中将$ window包含在$ scope旁边'TestContorller', function($scope, $window)并将警报调用更改为$window.alert('action handler called') ...

Just remove the {{}} and invoke the function within ng-click 只需删除{{}}并在ng-click调用该函数

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

DEMO DEMO

If I were you, I will do it like this 如果我是你,我会这样做

For html: 对于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. 使用ng-bind代替花括号。 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. 如果您需要用于特定测试的处理程序,则只需将一个ID(例如)传递给处理程序。

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.com/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 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM