返回对象时的Angular infdig错误

Angular infdig Error When Returning Object

本文关键字:infdig 错误 Angular 对象 返回      更新时间:2023-09-26

我是Angular的新手,遇到了这样的情况:我必须在ng-repeat中将一个对象传递给元素的属性。如果我不使用ajax获取数据,只返回一个普通对象,那么在返回新对象时代码可以正常工作,但是,当我这样做时,就会得到一个错误。我的问题是什么是最好的方法来做我正在做的代码下面,也解决我的错误。提前谢谢你。

HTML元素内ng-repeat:

<div ng-if="currentView.comparisons !== 'null'">
    <div ng-repeat="comparison in currentView.comparisons track by $index" class="row x-margintop">
     <div class="row x-margintop">
      <div class="col-md-8">
       <fusioncharts width="100%" height="400px" type="column2d" datasource="{{getComparisonChart(comparison)}}"></fusioncharts>
      </div>
    </div>
   </div>
</div>

JS:

 /*Ajax For Getting Comparisons Omitted For Brevity But Comparisons Will Have 
 The Following Properties*/
   var newCompObj = {name: comparison.name, template : comparison.template, dataSource: comparison.dataSource, date: {startDate: null, endDate: null}};
  $scope.currentView.comparisons.push(newCompObj);
$scope.getComparisonChart = function(chart){
//Callback and Other Chart Data Will Vary Based On The Chart Type
if(chart.dataSource === 'SvC'){
var data = [];
/* here I want to make a callback and return some data and have that data as part of the returned object.  I know this code is wrong, but not sure how to do this */
      $http({
  url:'callbacks/chart_data/dogCallback.jsp',
  method: 'GET',
  params: {startDate: chart.startDate, endDate: chart.endDate}
}).success(function(data){
    setData(data);  
});  
function setData(data){     
  return {
    "chart": {
      "caption": "Monthly revenue for last year",
      "subCaption": "Harry's SuperMart",
      "xAxisName": "Month",
      "yAxisName": "Revenues (In USD)",
      "numberPrefix": "$",
      "paletteColors": "#0075c2",
      "bgColor": "#ffffff",
      "borderAlpha": "20",
      "canvasBorderAlpha": "0",
      "usePlotGradientColor": "0",
      "plotBorderAlpha": "10",
      "placevaluesInside": "1",
      "rotatevalues": "1",
      "valueFontColor": "#ffffff",
      "showXAxisLine": "1",
      "xAxisLineColor": "#999999",
      "divlineColor": "#999999",
      "divLineDashed": "1",
      "showAlternateHGridColor": "0",
      "subcaptionFontBold": "0",
      "subcaptionFontSize": "14",
      "theme": "fint"
    },
    "data": data,
  }; 
}  
};

问题是我可以有许多fusioncharts,所以我不会有$scope。每一个都是Var。用户可以使用该图表类型创建任意数量的图表,并且该类型的每个图表都需要相同的对象,但将具有不同的数据。关于如何解决这个问题,我愿意接受任何想法。总之,我有一个元素需要一个对象作为它的数据源。图表的类型定义了对象和我使用ng-repeat重复的对象的其他属性,定义了必须通过回调返回的数据的参数。我还尝试了对象内嵌的Ajax调用,但没有成功。

如果您还需要其他信息请告诉我。

最后回到这个问题。不确定这对其他人是否有帮助。我最终做的是使用一个null数据对象的对象,然后在ng-repeat完成后设置数据对象。

if(startDate != null && endDate != null && (vsData != null && vsData.length > 0)){
  if(comparison.dataSource === 'svcChart'){
    $http({
      url:'callbacks/chart_data/Sales_StoreVsCountry.jsp',
      method: 'GET',
      params: {store: comparison.store, country: vsData, startDate: startDate, endDate: endDate}
    }).success(function(data){
      var chartObj = $scope.svcChart;
      chartObj.data = data;
      FusionCharts(chartId).setJSONData(chartObj);
    });
  }
}