如何将值从服务传递给控制器?我一直在阅读关于这方面的堆积如山的问题,而这些解决方案似乎都解决不了我的问题。我试图使用tabletop.js访问google电子表格,当我从服务中访问console.log时,我可以看到这些值,但是当我尝试从控制器访问电子表格值时,我会得到以下错误: chartService.getProperty不是一个函数
获取电子表格URL的代码运行良好。用get方法。不知道我在这里做错了什么。
控制器
angular.module('myapp')
.controller('piechartCtrl', function (chartService, $scope, config) {
$scope.values = chartService.getProperty();
});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) {
return{
getProperty: function(){
return data
},
setProperty: function(value){
data = value;
}
}
}
});发布于 2016-01-07 16:43:14
这是您的服务,我看到您返回的唯一东西是getUrl。因此,您能够从控制器访问的唯一东西是chartService.getUrl函数。
service('chartService', function ()
{
return
{
getUrl: function init(path)
{
Tabletop.init({
key: path,
callback: showInfo,
simpleSheet: true
})
}
}
function showInfo(data, tabletop)
{
return
{
getProperty: function ()
{
return data
},
setProperty: function (value)
{
data = value;
}
}
}
});为了让它起作用,虽然我不认为这是理想的解决方案,但它应该有效.
service('chartService', function ()
{
var returnObject =
{
getUrl: function init(path)
{
Tabletop.init({
key: path,
callback: showInfo,
simpleSheet: true
})
},
resultValue: {}
}
function showInfo(data, tabletop)
{
return
{
getProperty: function ()
{
return data
},
setProperty: function (value)
{
data = value;
returnObject.resultValue = value;
}
}
}
return returnObject
});然后将chartService.getProperty()替换为chartService.resultValue,尽管这是在no中是同步的。
https://stackoverflow.com/questions/34660422
复制相似问题