嗨,我正在学习有关angularjs的知识。我想从一个json获取TEMP,并在我的组件中显示该值。我不知道我在做什么错

问题描述 投票:0回答:1

我正在学习js提取。我试图从响应中获取temp的值,并在我的component.js中显示该值]

weather.component.js
```    
    myApp.component('weatherComponent', {
        template:"<p>Weather: {{vm.weather}}</p>",
        controllerAs: 'vm',
        controller: function WeatherController($scope,$element, $attrs) {
          vm = this;


          var apikey = "";
          var city = "London";


          fetch("https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid=" + apikey)
          .then(function(response) {
            vm.weather = response.main.temp;
            console.log("weather: "+ vm.weather);
            return response.json();
          })
          .then(function(myJson) {
            console.log(myJson);
          });

        }
      });
```

index.html
```
<weather-component></weather-component>
<script src="utils.js"></script>
<script src="weather.component.js"></script>
 ```

In utils it is initialized var myApp = angular.module('myApp', []);

API响应URL:https://openweathermap.org/current


我正在学习js提取。我正在尝试从响应中获取temp的值并将其显示在我的component.js weather.component.js```myApp.component('weatherComponent',{...

angularjs rest fetch-api openweathermap
1个回答
0
投票
    要从json获取元素:
© www.soinside.com 2019 - 2024. All rights reserved.