1

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

4
  • 5
    A quick google search, gave me 4 duplicates from Stackoverflow alone, 1, 2, 3, 4. Commented Apr 23, 2014 at 15:58
  • @MohammadSepahvand sorry i really appreciate you searching, but my problem is quite specific i have tried searching for it on google. Commented Apr 23, 2014 at 15:59
  • @user2681696 its not even a bit specific. Its exactly the same problem. Use services and you are done. Commented Apr 23, 2014 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. Commented Apr 23, 2014 at 16:01

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.