如何将数据值从serivces传递给控制器

时间:2016-01-15 11:54:11

标签: javascript jquery angularjs tabletop.js

我正在尝试为angular dashboard frameworksample widgets can be found here创建一个小部件。我很震惊,我试图在桌面小部件中集成tabletop.js。我不知何故无法获取控制器中的数据。

我的代码:

use strict;

angular.module('adf.widget.charts', ['adf.provider', 'highcharts-ng'])
  .config(function(dashboardProvider){
    var widget = {
      templateUrl: '{widgetsPath}/charts/src/view.html',
      reload: true,
      resolve: {
        /* @ngInject */
        urls: function(chartService, config){
          if (config.path){
            return chartService.getSpreadsheet(config.path);
          }
        }
      },
      edit: {
        templateUrl: '{widgetsPath}/charts/src/edit.html'
      }
  };

  dashboardProvider
      .widget('piechart', angular.extend({
        title: 'Custom Piechart',
        description: 'Creates custom Piechart with Google Sheets',
        controller: 'piechartCtrl'
        }, widget));
  });

服务和控制器

'use strict';

    angular.module('adf.widget.charts')
       .service('chartService', function ($q){
        return {
          getSpreadsheet: function init(path){
            var deferred = $q.defer();
            Tabletop.init({
              key: path,
              callback: function(data, Tabletop) {
                deferred.resolve([data, Tabletop]);
          },
              simpleSheet: true,
              debug: true
            });
          }
        }

      })


      .controller('piechartCtrl', function ($scope, urls) {
       $scope.urls = urls;
       console.log(data) //I get error here data is not defined

    });

edit.html

<form role="form">
  <div class="form-group">
    <label for="path">Google Spreadsheet URL</label>
    <input type="text" class="form-control" id="path" ng-model="config.path" placeholder="Enter URL">
  </div>
</form>

view.html

<div>
  <div class="alert alert-info" ng-if="!chartConfig">
    Please insert a repository path in the widget configuration
  </div>
  <div ng-if="chartConfig">
    <highchart id="chart1" config="chartConfig"></highchart>
      </div>
</div>

此时我只是尝试console.log来自piechartCtrl的数据,但我无法这样做。我在控制台中唯一得到的是

您传递了新的Google Spreadsheets网址作为密钥!试图解析。 tabletop.js:400使用密钥1voPzG2Sha-BN34bTK2eTe-yPtaAW2JZ16lZ3kaDWxCs进行初始化

这意味着在我输入图表网址时处理图纸,但是我无法从这一点开始。 Googlesheet URL

https://docs.google.com/spreadsheets/d/1voPzG2Sha-BN34bTK2eTe-yPtaAW2JZ16lZ3kaDWxCs/pubhtml

1 个答案:

答案 0 :(得分:0)

您可以使用$ broadcast服务并在其中传递对象。 例如,您可以通过以下方式从控制器广播事件:

$rootScope.$broadcast("sendingItemFromService",{"item": yourValue});

并且在其他控制器中,您甚至可以通过以下方式捕获它:

$scope.$on("sendingItemFromService", function(event, args) {
console.log(args.item);
});