首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将数据值从串行传递给控制器

如何将数据值从串行传递给控制器
EN

Stack Overflow用户
提问于 2016-01-15 11:54:10
回答 1查看 132关注 0票数 1

我正试图为角形仪表板框架这里可以找到样例小部件制作一个小部件。当我试图在小部件中集成tabletop.js时,我感到震惊。我不知何故无法得到控制器中的数据。

我的代码:

代码语言:javascript
复制
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));
  });

服务和控制器

代码语言:javascript
复制
'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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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电子表格url作为关键!试图解析。用键1voPzG2Sha-BN34bTK2eTe-yPtaAW2JZ16lZ3kaDWxCs初始化tabletop.js:400

这意味着,当我输入工作表url时,工作表将被处理,但我不能从这里开始。Googlesheet表URL

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

EN

回答 1

Stack Overflow用户

发布于 2016-01-15 12:09:56

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

代码语言:javascript
复制
$rootScope.$broadcast("sendingItemFromService",{"item": yourValue});

在其他控制器中,您甚至可以通过以下方式捕捉到这一点:

代码语言:javascript
复制
$scope.$on("sendingItemFromService", function(event, args) {
console.log(args.item);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34810521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档