如何将值从回调函数传递到控制器

时间:2016-01-14 13:21:42

标签: javascript angularjs json angular-controller

如何使用控制器访问服务返回的值。在我的代码中,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)

    }]);

3 个答案:

答案 0 :(得分: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中的参数想要什么,但是这样可以让你在正确的方向上找到一个好方法。

答案 1 :(得分:1)

最好的方式是与Promise。 在角度上,您将q框架设为$ q service $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

答案 2 :(得分:-2)

脏话:你可以使用广播和发射

在服务中:

$rootScope.$broadcast('myEvent', JSONSTUFF);
控制器中的

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