2014-05-17 175 views
2

我在控制器内部使我的函数正常工作时出现问题。AngularJS通过ng-click调用控制器内部的函数

给出的以下部分:

<div ng-controller="KundeDetailCtrl"><table class="table table-hover"> 
    <thead> 
    <tr> 
     <th>Name</th> 
     <th>InstallationsID</th> 
    </tr> 
    </thead> 
    <tbody > 

    <tr data-ng-repeat="mandant in kunde.mandanten" ng-click="getMandant(mandant)" > 

     <td> {{mandant.name}}</td> 
     <td>{{mandant.id}}</td> 

    </tr> 
    </tbody> 


</table></div> 

我希望能够点击一个行并调用相应的功能在我的控制器:

var AppControllers = angular.module('AppControllers', []); 


AppControllers.controller('KundeDetailCtrl', ['$scope', '$routeParams', 'Kunde', 
    function($scope, $routeParams, Kunde) { 
$scope.kunde = Kunde.get({kundeId: $routeParams.kundeId}, function(kunde) { 

}); 

    $scope.getMandant = function(id){ 

     for(var i= 0, l=$scope.kunde.mandanten.length; i<l; i++){ 
      if($scope.kunde.mandanten[i].id == "id") 
      { 
       $scope.mandant = $scope.kunde.mandanten[i]; 

      } 
     } 
     Location.href='index.html#/kunden/{{kunde._id}}/module' 
    }; 


    }]); 

其实,我只想要知道哪一行被点击并将点击行的对象移动到下一个应该显示其他数据的部分。

ng-click似乎没有做任何事情。在控制台我只看到getMandant: null

任何人都可以帮助我吗?

+0

你需要知道ngRepeat中行的$ index吗? – Dalorzo

+0

在你的ng-repeat中,你传递了整个对象,但是你用它来比较id。不应该在mandant.id上比较吗?或者在你的情况下,因为调用参数id.id?或者你可以保持功能的方式,但只能发送mandant.id ng-click – thsorens

+0

$ scope.getMandant = function(obj){console.log(obj)}在ng-click上返回null吗? – Dalorzo

回答

0

看来你是比较在mandaten列表字符串“ID”的ID,而不是对象ID:

if($scope.kunde.mandanten[i].id == "id") 

你也应该考虑使用===而不是==,它是在javascript中比较事物的首选方法。

看来你正在重定向到角度应用程序中的另一个视图,任何不使用$ location服务的理由?

相关问题