如何通过数据值从serivces到控制器 [英] How to pass data values from serivces to controller

查看:104
本文介绍了如何通过数据值从serivces到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让窗口小部件角度的仪表板框架, <一href=\"https://github.com/angular-dashboard-framework/angular-dashboard-framework/tree/master/sample/widgets\"相对=nofollow>样品部件都可以在这里找到。我在一个点,我想在窗口小部件tabletop.js整合来袭。我不知何故不能在控制器获取数据。

I am trying to make a widget for angular dashboard framework, sample widgets can be found here. I am struck at a point where I am trying to integrate tabletop.js in the widget. I somehow cannot get the data in the controller.

我的code:

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

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

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 ,但我不能这样做。我在控制台中看到的唯一的事情就是

At this point I am just trying to console.log data from piechartCtrl but I am unable to do so. The only thing I get in the console is

您通过了一项新的谷歌小号preadsheets网址的关键!试图解析。
tabletop.js:400与关键1voPzG2Sha-BN34bTK2eTe-yPtaAW2JZ16lZ3kaDWxCs初始化

这意味着,纸张被处理为我输入网址板,但我不能从这个角度获得成功。 Googlesheet网址

This means that the sheets are processed as I enter sheet url but I cannot get ahead from this point. Googlesheet URL

https://docs.google.com/s$p$padsheets/d/1voPzG2Sha-BN34bTK2eTe-yPtaAW2JZ16lZ3kaDWxCs/pubhtml

推荐答案

您可以使用$广播服务,并把它传递的对象。
例如,您可以按以下方式从控制器广播事件:

You can use $broadcast service and pass object in it. For example, you can broadcast event from a controller in following way:

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

和其他控制器,可以赶上这即使在下列方式:

and in other controller, you can catch this even in following way:

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

这篇关于如何通过数据值从serivces到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆