0

同様の質問がたくさん見つかりましたが、私の問題に解決策を適用できませんでした。

だから私のAngularアプリではnvd3チャートを描いています。

サービスで get リクエストを実行していますが、ブラウザのネットワークから、想定どおりにチャート データを常に取得していることがわかります。

問題はgrunt serve、Angular アプリを起動するために実行しているときに、まだ 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 に答える