首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用angularJS 1.6在http get调用中显示数据

如何使用angularJS 1.6在http get调用中显示数据
EN

Stack Overflow用户
提问于 2017-01-19 18:50:49
回答 4查看 1.4K关注 0票数 0

我能够控制http调用的结果,但是当我绑定它以便我可以在html页面上查看时,我不能这样做。有人能帮我一下吗?我正在张贴控制器代码和下面的html。

控制器代码

代码语言:javascript
运行
复制
           //module
        var weatherApp = angular.module('weatherApp',['ngRoute' , 'ngResource']);


        //Routes
        //http://api.openweathermap.org/data/2.5/forecast/daily?APPID=metric

        weatherApp.config(function ($routeProvider) {

            $routeProvider
            .when('/', {
              templateUrl:'pages/home.htm',
              controller:'homeController'
            })
            .when('/forecast', {
              templateUrl:'pages/forecast.htm',
              controller:'forecastController'
            });
        });

        //services
        weatherApp.service('cityService' ,function() {
          this.city ="San Jose, CA";
        });


        //controllers
        weatherApp.controller('homeController' ,['$scope', 'cityService', function($scope , cityService) {

          $scope.city = cityService.city;
          $scope.$watch('city' , function () {
            cityService.city = $scope.city;
          });
        }]);

          $scope.city = cityService.city;
  console.log($scope.city);
  $http({
      url: "http://api.openweathermap.org/data/2.5/forecast/daily?APPID==metric",
      method: "GET",
      params: {q: $scope.city, cnt:2}
   }).then(function(response){
        $scope.weatherResults = response.data; 
        console.log($scope.weatherResults);
   });

}]);

Index.htm的HTML代码

代码语言:javascript
运行
复制
  <!DOCTYPE html>
<html lang="en" ng-app="weatherApp">
<head>
  <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet"  href="css/navbar.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-route.min.js"></script>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-resource.min.js"></script>
   <script src="app.js"></script>
</head>
<body>
  <!-- NAVBAR -->
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
      <a class="navbar-brand" href="#">Accurate Weather</a>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Home</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
<!-- NAVBAR ENDS-->

<div class="container">
  <div ng-view></div>
</div>

</body>
</html>

Forecast.html的代码

代码语言:javascript
运行
复制
Forecast for {{city}}   <!-- THIS WORKS -->

{{ weatherResult.cnt }}  <!-- THIS DOES NOT WORK -->

home.htm的代码(为了清晰起见,这里一切正常)

代码语言:javascript
运行
复制
<div class = "row">
  <div class = "col-md-6 col-md-offset-3">
    <h4>Forecast by city</h4>
    <div class="form-group">
      <input type="text" ng-model="city" class="form-control" />
    </div>
      <a href="/weatherApp/index.htm#!/forecast" class="btn btn-primary">Get forecast </a>
  </div>
</div>

console output correctly displays the objects:

EN

回答 4

Stack Overflow用户

发布于 2017-01-19 18:56:21

也许可以尝试在控制器中将$scope.weatherResult初始化为空对象,以便AngularJS可以正确地查看它。

票数 0
EN

Stack Overflow用户

发布于 2017-01-19 19:01:07

只需将此赋值放在$scope.apply中,以通知angular引擎数据已异步更改,以便正确处理

代码语言:javascript
运行
复制
$scope.$apply(function () {
    $scope.weatherResult = response.data; //IF I LOG THIS IT WORKS
});

还可以添加检查,确认现在没有进行最大周期

代码语言:javascript
运行
复制
if(!$scope.$$phase) {
  //$digest or $apply
}
票数 0
EN

Stack Overflow用户

发布于 2017-01-19 19:02:49

Angular内置的双向数据绑定不能处理http响应,这是一个已知的问题。使用$timeout手动启动双向数据绑定,从而启动摘要周期。

代码语言:javascript
运行
复制
$scope.getWeather= function(){
    $http({
        url: "http://api.openweathermap.org/data/2.5/forecast/daily?AP&units=metric",
        method: "GET",
        params: {q: $scope.city, cnt:2}
 })
    .then(function(response){
           $timeout(function(){
             $scope.weatherResult = response.data; 
          }, 0);

    });
 }
}]);

注意,$timeout定时器设置为0ms,以立即启动摘要周期。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41739956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档