如何使用AngularJS将数据传递到另一个控制器?

时间:2014-04-23 15:55:12

标签: javascript angularjs

我想尝试传递从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 
    }

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>

我使用ng-repeat列出了一些汽车并且该位工作正常,但是当我点击clickThatCar元素时,我想只传递仅被点击的汽车的数据。我真的很有角度:(

1 个答案:

答案 0 :(得分:1)

您可以在ng-click来电中传递汽车信息。你的代码看起来像这样:

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