I've got a problem with getting my function inside my controller to work properly.

given the following partial:

<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>

I want to be able to click on one row and call the appropriate function in my controller:

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'
  };


  }]);

Actually, I only want to know which row was clicked and hand the object of the clicked row to the next partial which is supposed to display some other data.

The ng-click does not seem to do anything at all. In the console I only see getMandant: null

Can anyone help me with this?

share
    
do you need to know the $index of the row in the ngRepeat? – Dalorzo May 17 '14 at 21:28
    
in your ng-repeat, you pass in the entire object, but you use it to compare to id. Shouldnt your compare be on mandant.id ? Or in your case since calling the param id, id.id ? Or you could keep the function the way it is, but only send in mandant.id from your ng-click – thsorens May 17 '14 at 21:30
    
$scope.getMandant = function(obj){ console.log(obj) } does this return null on ng-click? – Dalorzo May 17 '14 at 21:33
    
the console returns the correct id (after I corrected the code like thsorens suggested), but still, the Location.href does not happen. – hqbloomy May 17 '14 at 21:39
    
it worked to some extent: I managed to get everything to the console, but the $scope.mandant and the getMandant are still null in the new partial html. Do you have any suggestions? – hqbloomy May 17 '14 at 22:23

It seems you are comparing the id in the mandaten list to the string "id" rather than the object id:

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

You should also consider using === rather than ==, it is the preferred way to compare things in javascript.

It appears you are redirecting to another view in your angular application, any reason not to use the $location service?

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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