0

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

所以在我的角度应用程序中,我正在绘制 nvd3 图表。

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

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

这只发生在我跑步时grunt serve。但是,如果我点击刷新,在运行 grunt serve 后,数据会正确显示。

提前感谢您的帮助。

这就是我想要做的:

'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;
  }
});
4

1 回答 1

0

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


.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; } });

这可能有效,取决于您如何设置 nvd3 图表。

于 2014-09-18T13:31:15.723 回答