Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I want to try to pass data that was taken from an api:

Javascript:

 function RecentCars($scope, $http){
        $scope.cars = [];
        $http({method:'GET'; url:'/recent'}).success(function(data,status){
                $scope.cars = data;
        });
        $scope.clickThatCar =function(){
                // want to pass that cars data to the CarPage Controller

        }
    }

    function CarPage($scope, data){
       // get the data for the car that was clicked 
    }

The HTML:

<div ng-repeat="motor in cars">
       <div class="car_row" ng-click="clickThatCar()">
           <img class="car_img" src="/images/{{motor._id}}_0.jpg">
           <div class="car_info">
               <span class="brand_name">{{motor.brand}}</span>
               <span class="price">4,200 KD</span>
          </div>
       </div>
</div>

Im listing some cars using ng-repeat and that bit works fine, however when i click clickThatCar element, I want to just pass the data for the car that was clicked only. Im really new to angular :(

share|improve this question

marked as duplicate by Mohammad Sepahvand, sp00m, Andrew Whitaker, Der Golem, 48klocs Apr 23 '14 at 17:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5  
A quick google search, gave me 4 duplicates from Stackoverflow alone, 1, 2, 3, 4. –  Mohammad Sepahvand Apr 23 '14 at 15:58
    
@MohammadSepahvand sorry i really appreciate you searching, but my problem is quite specific i have tried searching for it on google. –  user2681696 Apr 23 '14 at 15:59
    
@user2681696 its not even a bit specific. Its exactly the same problem. Use services and you are done. –  Fuzzyma Apr 23 '14 at 16:00
    
Nope, your problem is not specific at all from what you describe, just declare some backing variable in a service and share that service between your controllers, as described here. –  Mohammad Sepahvand Apr 23 '14 at 16:01

1 Answer 1

up vote 1 down vote accepted

You can pass the car info in the ng-click call. Your code would look like this:

<div ng-repeat="motor in cars">
    <div class="car_row" ng-click="clickThatCar(motor)">
        <img class="car_img" src="/images/{{motor._id}}_0.jpg">
        <div class="car_info">
            <span class="brand_name">{{motor.brand}}</span>
            <span class="price">4,200 KD</span>
        </div>
    </div>
</div>
share|improve this answer
    
Thanks that helped :) –  user2681696 Apr 23 '14 at 16:09

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