gpt4 book ai didi

javascript - 使用 AngularJS 处理 Json 数据

转载 作者:行者123 更新时间:2023-11-28 19:10:57 25 4
gpt4 key购买 nike

我有一个新案例,需要帮助。我想要有 9 个按钮和一个显示电影详细信息的面板,具体取决于单击的按钮。所以说,如果我单击“变压器”,变压器详细信息应该出现在面板中。然后,如果我单击“Fury”,面板详细信息将更改为Fury 详细信息。我需要将此数据保存在 JSON 文件中。我已经看过,如何做到这一点,并努力理解我将如何去做。这是我到目前为止所得到的。

JS:

var app = angular.module("myApp", []);

app.controller('MainController', ['$scope', function ($scope) {


}])

JSON:

{
"movie": [
{
"id": 1,
"name": "Star Wars The Phantom Menace",
"format": "DVD",
"rate": 4,
"price": 2
},
{
"id": 2,
"name": "Star Wars A New Hope",
"format": "DVD",
"rate": 6,
"price": 4
},
{
"id": 3,
"name": "Transformers",
"format": "Blu-Ray",
"rate": 7,
"price": 3
},
{
"id": 4,
"name": "Transformers Dark of the Moon",
"format": "Blu-Ray",
"rate": 6,
"price": 5
}
]}

HTML:

  <body ng-controller="MainController" ng-app="myApp">
<h1 style="text-align:center">Garrett's Rentals</h1>

<div ng-repeat="movie in movies">
<button>{{movie.name}}</button>
</div>

<div class="panel">
<h3>You have selected:</h3>
<p>{{movie.name}}</p>
<p>{{movie.format}}</p>
<p>{{movie.rate}}</p>
<p>{{movie.price}}</p>
</div>
</body>

最佳答案

使用

var app = angular.module("myApp", []);

app.controller('MainController', ['$scope', function ($scope) {
var json={
"movie": [
{
"id": 1,
"name": "Star Wars The Phantom Menace",
"format": "DVD",
"rate": 4,
"price": 2
},
{
"id": 2,
"name": "Star Wars A New Hope",
"format": "DVD",
"rate": 6,
"price": 4
},
{
"id": 3,
"name": "Transformers",
"format": "Blu-Ray",
"rate": 7,
"price": 3
},
{
"id": 4,
"name": "Transformers Dark of the Moon",
"format": "Blu-Ray",
"rate": 6,
"price": 5
}
]};
console.log(json);
$scope.movies=json.movie;
console.log($scope.movie);
}]);

HTML

    <body ng-controller="MainController" ng-app="myApp">
<h1 style="text-align:center">Garrett's Rentals</h1>

<div ng-repeat="movie in movies">
<button>{{movie.name}}</button>
</div>

<div class="panel">
<h3>You have selected:</h3>
<p>{{movie.name}}</p>
<p>{{movie.format}}</p>
<p>{{movie.rate}}</p>
<p>{{movie.price}}</p>
</div>
</body>

fiddle 支持:http://jsfiddle.net/sXkjc/993/

关于javascript - 使用 AngularJS 处理 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30728840/

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