gpt4 book ai didi

javascript - 如何在 Angularjs 中的 ng-repeat 中显示单行

转载 作者:行者123 更新时间:2023-12-02 14:01:58 24 4
gpt4 key购买 nike

我对 AngularJs 非常陌生,并尝试在我的项目中使用它。我制作了一个 WebApi 函数来返回城市的当前温度。我想从 html 获取单个温度并显示它。我知道 ng-repeat 用于显示许多项目,但如何显示单个项目。

我当前的代码如下。我希望将“20”显示为温度,但它在两行中显示“2”和“0”:)请帮忙。如何将其显示为单行中的“20”?

<div ng-controller="WearherController">
<table ng-repeat="weather in weathers" border="1">
<tr>
<td>{{weather}}</td>
</tr>
</table>
</div>
....
....
....
<script>
var app = angular.module("ZenHaberApp", []);
app.controller("WeatherController", function ($scope, $http) {
var city = 'Adana';
$http.get('http://localhost:62747/api/getweatherbycityname/' + city).
success(function (data, status, headers, config) {
$scope.weathers = data.condition_temp;

}).
error(function (data, status, headers, config) {
alert("error");
});
});
</script>

我认为@Sajeetharan 给了我解决方案,但现在它显示了一个愚蠢的图像。它是什么?我怎样才能解决这个问题? enter image description here

这是其余的代码

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script>
var app = angular.module("ZenHaberApp", []);
app.controller("WeatherController", function ($scope, $http) {
var city = 'Adana';
$http.get('http://localhost:62747/api/getweatherbycityname/' + city).
success(function (data, status, headers, config) {
$scope.weathers = data.condition_temp;

}).
error(function (data, status, headers, config) {
alert("error");
});
});
</script>

解决方案:

感谢@Sajeetharan,他发现了问题。当时是{{天气}}

应该是{{weathers}}

最佳答案

可以直接给$scope变量赋值weathers,

<table border="1">
<tr>
<td>{{weathers}}</td>
</tr>
</table>

<强> DEMO

关于javascript - 如何在 Angularjs 中的 ng-repeat 中显示单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40332134/

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