Javascript 如何将回调函数中的值从服务传递给控制器
如何使用控制器访问从服务返回的值。在my code service.js中,函数showInfo()返回JSON对象。但我无法访问此函数之外的这些对象。如果我尝试从controller.js执行console.log console.log(chartService.showInfo.new\u数据) 我明白了 错误无法读取未定义的属性“新数据”。 如果我尝试这样做,也会发生同样的情况 console.log(chartService.showInfo) 我得到未定义。 如何从控制器访问函数showInfo中的JSON对象new_数据 Service.jsJavascript 如何将回调函数中的值从服务传递给控制器,javascript,angularjs,json,angular-controller,Javascript,Angularjs,Json,Angular Controller,如何使用控制器访问从服务返回的值。在my code service.js中,函数showInfo()返回JSON对象。但我无法访问此函数之外的这些对象。如果我尝试从controller.js执行console.log console.log(chartService.showInfo.new\u数据) 我明白了 错误无法读取未定义的属性“新数据”。 如果我尝试这样做,也会发生同样的情况 console.log(chartService.showInfo) 我得到未定义。 如何从控制器访问函数sh
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]]
};
}));
}
})
angular.module('myApp')
.controller('piechartCtrl', [ '$scope', 'chartService', function (chartService, $scope) {
console.log(chartService.showInfo.new_data)
}]);
Controller.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]]
};
}));
}
})
angular.module('myApp')
.controller('piechartCtrl', [ '$scope', 'chartService', function (chartService, $scope) {
console.log(chartService.showInfo.new_data)
}]);
服务
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')
.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 (chartService, $scope) {
var tabletop = chartService.init(),
chartInfo = chartService.showInfo(someData, tabletop);
console.log(chartInfo);
}]);
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)
})
}]);
我不知道您想要showInfo中的参数做什么,但这应该可以让您朝着正确的方向走。肮脏的方式:您可以使用广播和发射 在职期间:
$rootScope.$broadcast('myEvent', JSONSTUFF);
在控制器中:
$scope.$on("myEvent", function(e, json){
console.log(json);
});
最好的办法是有承诺。
在angular中,q框架作为$q服务
服务
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')
.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 (chartService, $scope) {
var tabletop = chartService.init(),
chartInfo = chartService.showInfo(someData, tabletop);
console.log(chartInfo);
}]);
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)
})
}]);
工作示例我在
showInfo
函数中没有看到返回语句。那<代码>返回代码>是地图功能。看看这个。。。我只想在showinfo中显示,基本上从showinfo中获取输出controller@Imo好吧,如果你读了我的代码,你可能会理解这是如何产生一个新问题的,我正在获取路径抛出getUrl函数resolve:{/*@ngInject*/url:function(chartService,config){if(config.path){return chartService.getURL(config.path);}}}},现在我无法获取电子表格的URL