如何通过休息客户端动态设置malhar角度仪表板中的de widgetDefinitions?

时间:2015-12-21 17:42:58

标签: angularjs rest angular-http angular-module

我已经安装了malhar-angular-dashboard模块,我想用其余数据填充一些 widgetDefinitions

HTML

<div ng-controller="widgetCtrl">
   <div  dashboard-layouts="layoutOptions" class="dashboard-container"></div>
</div>

widgetRestService

.factory('widgetRestService',['$http','UrlService','$log','$q',
   function($http,UrlService,$log,$q){

  var serviceInstance = {};

  serviceInstance.getInfo = function(){
    var request = $http({method: 'GET', url: '/rest/widgets/getListInfoDashboards'})
      .then(function(success){
        serviceInstance.widgets = success.data;
        $log.debug('serviceInstance.widgets SUCCESS',serviceInstance.widgets);
      },function(error){
        $log.debug('Error ', error);
        $log.debug('serviceInstance.widgets ERROR',serviceInstance.widgets);
      });
    return request;
  };

  serviceInstance.getAllWidgets = function () {
    if (serviceInstance.widgets) {
      return serviceInstance.widgets;
    } else {
      return [];
    }
  };

  return serviceInstance;

}])

我的休息服务会返回此 3个对象的数组[{"name":"widgetList","title":" "},{"name":"widgetPie","title":" "},{"name":"widgetTable","title":" "}]

OtherService

.factory("OtherService", ["widgetRestService", "$log", "$q",
   function (widgetRestService, $log, $q) {

  var deferred = $q.defer();

  widgetRestService.getInfo().then(function () {

    deferred.resolve(widgetRestService.getAllWidgets());
  });

  return deferred.promise;

}])

控制器

OtherService.then(function(response){
    $scope.layoutOptions = { // layout with explicit save
      storageId: 'demo-layouts-explicit-save',
      storage: localStorage,
      storageHash: 'fs4df4d51',
      widgetDefinitions:response , //must be a list
      defaultWidgets: [],
      explicitSave: true,
      defaultLayouts: [
        {title: 'Layout 1', active: true, defaultWidgets: []}
      ]
    };
    $log.debug('layoutOptions =',$scope.layoutOptions);
  });

结果

layoutOptions: Object{defaultLayouts:Array[1],
defaultWidgets:Array[0],
explicitSave:true,
storage:Storage,
storageHash:'fs4df4d51',
storageId: 'demo-layouts-explicit-save', 
widgetDefinitions: Array[3]}
  

TypeError:无法读取属性&#39; $$ hashKey&#39;未定义的       在Object.extend(http://localhost:9000/bower_components/angular/angular.js:406:14)       在Object.LayoutStorage(http://localhost:9000/bower_components/malhar-angular-dashboard/dist/malhar-angular-dashboard.js:1064:15

我在第2行搜索:Object.LayoutStorage,我发现了这个:

  angular.extend(defaults, options); //options = undefined (should have some keys and my widgetDefinitions array)
  angular.extend(options, defaults);

仅当我想在然后回调函数中设置 $ scope.layoutOptions 时,选项变量才会定义。 有任何建议如何设置/避免这个?

1 个答案:

答案 0 :(得分:1)

问题是dashboard-layouts指令将在OtherService的异步调用完成之前编译,这意味着$scope.layoutOptions将是undefined

一个简单的解决方案是在dashboard-layouts可用之前防止$scope.layoutOptions指令编译。

您可以使用ng-if

执行此操作
<div ng-if="layoutOptions" dashboard-layouts="layoutOptions" class="dashboard-container">
</div>