【问题标题】:how to pass values from callback function from service to controllers如何将回调函数中的值从服务传递到控制器
【发布时间】:2016-01-14 13:21:41
【问题描述】:

如何使用控制器访问从服务返回的值。在我的代码 service.js 函数 showInfo() 返回 JSON 对象。但是我不能在这个函数之外访问这些对象。如果我尝试从 controller.js 执行 console.log

console.log(chartService.showInfo.new_data)

我明白了

错误无法读取未定义的属性“new_data”

如果我尝试也会发生同样的情况

console.log(chartService.showInfo)

我得到未定义

如何从控制器访问函数 showInfo 内的 JSON 对象 new_data?

Service.js

angular.module('myApp')
   .service('chartService', function (){
  return {
     getUrl: function init(path) {
        Tabletop.init( { key: path,
                         callback: showInfo,
                         simpleSheet: true } )
     }
  }

     function showInfo(data, tabletop){

          var new_data = JSON.stringify(data.map(function(el) {
            return {
                "name": el[Object.keys(el)[0]],
                "y": +el[Object.keys(el)[1]]
            };
     }));
   }
})

Controller.js

angular.module('myApp')    
      .controller('piechartCtrl', [ '$scope', 'chartService',  function (chartService, $scope) {
        console.log(chartService.showInfo.new_data)
    
    }]);

【问题讨论】:

标签: javascript angularjs json angular-controller


【解决方案1】:

服务

angular.module('myApp').service('chartService', function (){
    return {
        init: init,
        showInfo: showInfo
    };

    function init(path) {
        return Tabletop.init({
            key: path,
            callback: showInfo,
            simpleSheet: true 
        });
    }

    function showInfo(data, tabletop){
        return JSON.stringify(data.map(function(el) {
            return {
                "name": el[Object.keys(el)[0]],
                "y": +el[Object.keys(el)[1]]
            };
        }));
    }
});

控制器

angular.module('myApp').controller('piechartCtrl', [ '$scope', 'chartService',  function (chartService, $scope) {
    var tabletop = chartService.init(),
        chartInfo = chartService.showInfo(someData, tabletop);

    console.log(chartInfo);        
}]);

我不确切知道您对 showInfo 中的参数想要什么,但这应该可以让您朝着正确的方向前进。

【讨论】:

  • 我只想在 showinfo 中显示它们基本上是从控制器内部的 show info 中获取输出
  • @Imo 好吧,如果您阅读我的代码,您可能会明白如何
  • 这产生了一个新问题,我得到了 Paths throw getUrl function resolve: { /* @ngInject */ urls: function(chartService, config){ if (config.path){ return chartService.getUrls (配置路径); } } },现在我无法获取电子表格的网址
【解决方案2】:

最好的方法是使用 Promise。 在 Angular 中,您将 q 框架作为 $q 服务 $q docs

服务

angular.module('myApp')
  .service('chartService', function($q) {
    var deferredSpreadsheet = $q.defer();
    return {
      getSpreadsheet: function init(path) {

        Tabletop.init({
          key: path,
          callback: showInfo,
          simpleSheet: true
        });
        return deferredSpreadsheet.promise;

      },

    }

    function showInfo(data, tabletop) {

      data = JSON.stringify(data.map(function(el) {
        return {
          "name": el[Object.keys(el)[0]],
          "y": el[Object.keys(el)[1]]
        };
      }));
      deferredSpreadsheet.resolve(data);

    }
  })

控制器

angular.module('myApp')
.controller('piechartCtrl', ['$scope', 'chartService', function($scope, chartService) {

    var path = "https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AmYzu_s7QHsmdDNZUzRlYldnWTZCLXdrMXlYQzVxSFE&output=html";
    var pro = chartService.getSpreadsheet(path).then(function(data) {
      console.log(data)

    })


  }]);

工作示例here

【讨论】:

    【解决方案3】:

    肮脏的方式:你可以使用广播和发射

    在服务中:

    $rootScope.$broadcast('myEvent', JSONSTUFF);
    

    在控制器中:

    $scope.$on("myEvent", function(e, json){
    console.log(json);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-18
      • 2017-03-23
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      相关资源
      最近更新 更多