首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过服务的angular http get请求需要刷新以显示数据

通过服务的angular http get请求需要刷新以显示数据
EN

Stack Overflow用户
提问于 2014-09-18 20:07:59
回答 1查看 216关注 0票数 0

我发现了许多类似的问题,但我无法将解决方案应用于我的问题。

因此,在我的angular应用程序中,我正在绘制nvd3图表。

我正在服务中执行get请求,并且我可以从浏览器中的网络中看到,我总是按照预期获得图表数据。

问题是,当我运行grunt serve启动我的angular应用程序时,我仍然通过api获取数据,但由于某种原因,它们没有显示出来。

这种情况只有在我运行grunt serve时才会发生。但是,如果我点击刷新,在运行grunt serve之后,数据可以正确显示。

提前感谢您的帮助。

这就是我想要做的:

代码语言:javascript
复制
'use strict';

angular.module('myApp')
.service('mainService', function($http) {
  this.getData = function() {
    return $http({
      method: 'GET',
      url: '/rest/api/config',
    });
  }
})
.controller('MainCtrl', function($scope, $http, mainService) {
  var name = 'main';
  var model; 

  mainService.getData().then(function(d) {
    model = d.data;
    $scope.modelling();
  });

  $scope.modelling = function() {
    if(!model) {
      console.log("no model");
      // set default model for demo purposes
      model = {
        title: "about",
        structure: "12/4-4-4",
    };
  }

  console.log(model);
  $scope.name = name;
  $scope.model = model;
  $scope.collapsible = true;
  }
});
EN

回答 1

Stack Overflow用户

发布于 2014-09-18 21:31:16

试试这样的东西。最初,在您的示例中,$scope.model将是未定义的。

代码语言:javascript
复制
.controller('MainCtrl', function($scope, $http, mainService) {
  var name = 'main';

mainService.getData().then(function(d) {
    $scope.modelling(d.data);
  });

$scope.modelling = function(data) {
    //do something with data here, or set 
    $scope.model = data;
  }

$scope.name = name;
  $scope.collapsible = true;
}
});

That might work, depends on how you set up the nvd3 charts.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25912199

复制
相关文章

相似问题

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