gpt4 book ai didi

javascript - 如何使用 AngularJS 将数据传递到另一个 Controller ?

转载 作者:行者123 更新时间:2023-11-28 01:21:03 24 4
gpt4 key购买 nike

我想尝试传递从 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 元素时,我只想传递仅被单击的汽车的数据。我对 Angular 真的很陌生:(

最佳答案

您可以在 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>

关于javascript - 如何使用 AngularJS 将数据传递到另一个 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23249505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com