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

Unknown
  • 257
  • 1
  • 3
  • 10
  • 5
    A quick google search, gave me 4 duplicates from Stackoverflow alone, [1](http://stackoverflow.com/questions/21919962/angular-share-data-between-controllers), [2](http://stackoverflow.com/questions/16508666/sharing-a-variable-between-controllers-in-angular-js), [3](http://stackoverflow.com/questions/18227090/angularjs-sharing-data-between-controllers), [4](http://stackoverflow.com/questions/20181323/passing-data-between-controllers-in-angular-js). – 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. – Unknown 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](http://stackoverflow.com/a/21452417/189756). – Mohammad Sepahvand Apr 23 '14 at 16:01

1 Answers1

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>
reaxis
  • 1,361
  • 12
  • 11