2014-05-17 217 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服務的理由?

相關問題