Angularjs 一个接一个的角度渲染模型
假设我有一个带有汽车列表的JSON:Angularjs 一个接一个的角度渲染模型,angularjs,views,render,Angularjs,Views,Render,假设我有一个带有汽车列表的JSON: { "company": "Mercedes", "cars": [{ "idCar": 1, "Name": car 1, "img":"..." },{ "idCar": 2, "Name": car 2, "img":"..." },{ "idCar": 3, "Name": car 3, "img":"..." },{ "idCar": 4,
{
"company": "Mercedes",
"cars":
[{
"idCar": 1,
"Name": car 1,
"img":"..."
},{
"idCar": 2,
"Name": car 2,
"img":"..."
},{
"idCar": 3,
"Name": car 3,
"img":"..."
},{
"idCar": 4,
"Name": car 4,
"img":"..."
},{
"idCar": 5,
"Name": car 5,
"img":"..."
}]
}
我正在做一个角度应用程序,用户可以按下按钮来显示这些汽车,但用户一次只能看到一辆汽车,当他看到这辆汽车的规格后,他可以单击一个“下一步”按钮,然后显示下一辆汽车,依此类推
现在你们可能会注意到,我是Angular的初学者,我想知道是否有办法做到这一点
提前谢谢。这是一个非常基本的问题。这将使您朝着正确的方向前进:
app.controller("MyCtrl", ["$scope", function ($scope) {
$scope.company = {
company: "Mercedes",
cars: [
{ "idCar": 1, "Name": "car 1", "img": "http://lorempixel.com/200/200" },
{ "idCar": 2, "Name": "car 2", "img": "http://lorempixel.com/200/200" },
{ "idCar": 3, "Name": "car 3", "img": "http://lorempixel.com/200/200" },
{ "idCar": 4, "Name": "car 4", "img": "http://lorempixel.com/200/200" },
{ "idCar": 5, "Name": "car 5", "img": "http://lorempixel.com/200/200" }
]
};
$scope.currentIndex = 0;
$scope.currentCar = $scope.company.cars[$scope.currentIndex];
$scope.nextCar = function() {
$scope.currentIndex++;
if ($scope.currentIndex >= $scope.company.cars.length) {
$scope.currentIndex = 0;
}
$scope.currentCar = $scope.company.cars[$scope.currentIndex];
}
}]);
视图:
{{currentCar.Name}
下一个
示例:这是一个非常基本的角度问题。这将使您朝着正确的方向前进:
app.controller("MyCtrl", ["$scope", function ($scope) {
$scope.company = {
company: "Mercedes",
cars: [
{ "idCar": 1, "Name": "car 1", "img": "http://lorempixel.com/200/200" },
{ "idCar": 2, "Name": "car 2", "img": "http://lorempixel.com/200/200" },
{ "idCar": 3, "Name": "car 3", "img": "http://lorempixel.com/200/200" },
{ "idCar": 4, "Name": "car 4", "img": "http://lorempixel.com/200/200" },
{ "idCar": 5, "Name": "car 5", "img": "http://lorempixel.com/200/200" }
]
};
$scope.currentIndex = 0;
$scope.currentCar = $scope.company.cars[$scope.currentIndex];
$scope.nextCar = function() {
$scope.currentIndex++;
if ($scope.currentIndex >= $scope.company.cars.length) {
$scope.currentIndex = 0;
}
$scope.currentCar = $scope.company.cars[$scope.currentIndex];
}
}]);
视图:
{{currentCar.Name}
下一个
示例:您的JSON似乎是错误的。如果您的JSON看起来是错误的,那么名称字段值应该用引号括起来。如果名称字段值是stringaddition,那么它应该用引号引起来,这是一种非常简单的方法。你可以使用旋转木马。谢谢你的快速回复,这将非常有帮助。此外,这是一种非常简单的方式。您可以使用旋转木马。谢谢您的快速响应,这将非常有帮助。