gpt4 book ai didi

javascript - 导入 Json 数据并在 Angular 应用程序中显示

转载 作者:行者123 更新时间:2023-11-28 05:43:21 25 4
gpt4 key购买 nike

我一周前开始学习 Angular,现在我正在尝试自己构建一个应用程序(因为人们说这是最好的学习方式),我正在尝试做一件我以前没有做过的事情,所以不要这么粗鲁我,如果这很容易,我正在尝试构建一个图鉴,我正在尝试使用 Json 中已经存在的数据,并将其导入到 Angular Controller 中,这样我就可以使用 ng-repeat 来显示数据,但我不知道为什么它没有结果,也许我犯了很多错误,但我找不到它:/,我将在这里发布我所做的:

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="angular.js"></script>
<script src="pokedex.js"></script>

<body ng-app ="pokedex">
<div class="row">
<div class="col-md-offset-1 form-group">
<label for="sel1">Select list:</label>
<select class="form-control">
<option>All</option>
<option>Normal</option>
<option>Watter</option>
<option>Fire</option>
<option>Eletric</option>
<option>Rock</option>
<option>Ice</option>
<option>Grass</option>
<option>Psychic</option>
<option>Poison</option>
<option>Dragon</option>
</select>
</div>
</div>

<div class="row" ng-controller="PokemonController as pokedex">
<div class="col-md-offset-1 col-md-6" ng-repeat = "pokemon in pokedex.pokemons">
<p>Name: {{pokemon.Name}}</p>
</div>
</div>

脚本

(function(){

var app = angular.module('pokedex',[]);

app.controller('pokemonController',['$http',function($http){
var pokemons = this;
$http.get('https://pokedex-deluxor.rhcloud.com/getall').success(function(data){
pokemons = data;
});
}]);
});

Json 网址:Pokedex

Ps:对不起我的英语不好的 friend 们:/

最佳答案

ng-repeat = "pokemon in pokedex.pokemons"

您在 pokedex.pokemons 上使用 ng-repeat,您必须从获取数据的 Controller 将数据分配给 pokedex.pokemons。

 $http.get('https://pokedex-deluxor.rhcloud.com/getall').success(function(data){
// assign data here
});

关于javascript - 导入 Json 数据并在 Angular 应用程序中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38742218/

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