我的项目中有一个仪表图表控制器,它必须显示角度服务的数据。但是当我调用服务函数时,它不会接受来自服务的数据。这是我正在尝试的:
Controller.js
angular.module("app.controllers", [])
.controller("gaugeCtrl", ["$scope","config","myService",
function ($scope,config,myService) {
$scope.gaugeHome = {
gaugeData: myService.getdata(),
gaugeOptions: {
lines: 12,
angle: 0,
lineWidth: 0.47,
pointer: {
length: 0.6,
strokeWidth: 0.03,
color: "#555555"
},
limitMax: "false",
colorStart: config.secondary_color,
colorStop: config.secondary_color,
strokeColor: "#F5F5F5",
generateGradient: !0,
percentColors: [
[0, config.secondary_color],
[1, config.secondary_color]
]
}
}
])
这是我的服务数据。它确实获取数据但无法传递到控制器代码中的控制器 val 字段
var demoService= angular.module('demoService',[])
.service('myService',function($http,$q){
var deferred = $q.defer();
$http.get('http://enlytica.com/RSLivee/rest/census').then(function(data)
{
deferred.resolve(data);
/*var v=0;
$players=data.data;
console.log($players.Tweets);
($players.Tweets).forEach(function(index, element) {
v=v+ (index.FAVOURITE_COUNT);
console.log(index.FAVOURITE_COUNT);
});
console.log(v);
deferred.resolve(v);*/
});
this.getdata=function()
{
var dataarr = [];
var v=0;
deferred.promise.then(function(data)
{
$players=data.data;
console.log($players.Tweets);
($players.Tweets).forEach(function(index, element) {
v=v+ (index.FAVOURITE_COUNT);
console.log(index.FAVOURITE_COUNT);
});
console.log(v);
dataarr.push({ maxValue: 3e3,animationSpeed: 100,val: v})
console.log(dataarr);
});
return (dataarr);
}
})
我也尝试在控制器中获取数据,如下所示,但它也没有工作:
angular.module("app.controllers", []).controller("gaugeCtrl",["$scope","config","myService",
function ($scope,config,myService) {
var v=0;
var dataarr=[];
var promise=myService.getdata();
console.log(promise);
var tp=promise.then(function(data)
{
$players=data.data;
console.log($players.Tweets);
($players.Tweets).forEach(function(index, element) {
v=v+ (index.FAVOURITE_COUNT);
// console.log(index.FAVOURITE_COUNT);
});
console.log(v);
dataarr.push({ maxValue:800,animationSpeed: 100,val: v});
console.log(dataarr);
return dataarr;
});
]}
但无法访问 dataarr 或 v 值。有没有办法可以将价值传递给我的计量器。
提前致谢
@OrenD 我更改了Service.js代码:[我删除了DI,因为它引发了错误 - >
Error: [ng:areq] http://errors.angularjs.org/1.3.14/ng/areq?
p0=fn&p1=not%20aNaNunction%2C%20got%string
at Error (native)
at http://localhost:8080/Enlytica18thOnwards/dist/js/app.js:14:4847
at Sb (http://localhost:8080/Enlytica18thOnwards/dist/js/app.js:14:11576)
at tb (http://localhost:8080/Enlytica18thOnwards/dist/js/app.js:14:11670)
at Function.ab.$$annotate (http://localhost:8080/Enlytica18thOnwards/dist/js/app.js:16:25928)
at e (http://localhost:8080/Enlytica18thOnwards/dist/js/app.js:14:19914)
at Object.instantiate (http://localhost:8080/Enlytica18thOnwards/dist/js/app.js:14:20199)
at Object.<anonymous> (http://localhost:8080/Enlytica18thOnwards/dist/js/app.js:14:20480)
at Object.e [as invoke] (http://localhost:8080/Enlytica18thOnwards/dist/js/app.js:14:20075)
at Object.$get (http://localhost:8080/Enlytica18thOnwards/dist/js/app.js:14:19010) ]
Service .js
var demoService= angular.module('demoService',[])
.service('myService', function($http, $q) {
this.getData = function() {
return $http.get('http://enlytica.com/RSLivee/rest/census')
.then(function(data) {
var v = 0;
for (var i = 0; i < data.data.Tweets.length; ++i) {
v += data.data.Tweets[i]['FAVOURITE_COUNT'];
}
console.log(v);//doesnt show anything on console
return $q.when([{
maxValue: 3e3,
animationSpeed: 100,
val: v
}]);
});
};
}
);
但是现在这还没有从网址读取数据。我做错了什么?
答案 0 :(得分:1)
建议服务返回promises,因为在很多情况下,请求的数据是从后端异步获取的。
以下是服务的粗略实现,以提供您可以在控制器中使用的承诺:
app.service('myService', ['$http', '$q', function($http, $q) {
this.getData = function() {
return $http.get('http://enlytica.com/RSLivee/rest/census')
.then(function(data) {
var v = 0;
for (var i = 0; i < data.data.Tweets.length; ++i) {
v += data.data.Tweets[i]['FAVOURITE_COUNT'];
}
return $q.when([{
maxValue: 3e3,
animationSpeed: 100,
val: v
}]);
});
};
}
]);
现在,在控制器中,您必须根据所提供的承诺来解决问题。你有几种选择:
这是一个代码片段,显示了使用已解析的promise数据更新gaugeData的方法:
app.controller('gaugeCtrl', ['$scope', 'config', 'myService',
function($scope, config, myService) {
$scope.gaugeHome = {
gaugeData: [],
gaugeOptions: {
lines: 12,
angle: 0,
lineWidth: 0.47,
pointer: {
length: 0.6,
strokeWidth: 0.03,
color: "#555555"
},
limitMax: "false",
colorStart: config.secondary_color,
colorStop: config.secondary_color,
strokeColor: "#F5F5F5",
generateGradient: !0,
percentColors: [
[0, config.secondary_color],
[1, config.secondary_color]
]
}
}
myService.getData().then(function(dataArr) {
$scope.gaugeHome.gaugeData = dataArr;
});
}
]);
答案 1 :(得分:1)
试试这个: -
app.factory("companyData", function ($http) {
return {
getAll: function (items) {
var path = "https://dl.dropboxusercontent.com/u/28961916/companies.json"
$http.get(path).success(items);
}
}
});
letus consider this is a service
here this returns items object
if I want to use that items object in controller app.controller("mainController", function ($scope, companyData) {
//console.log(companyData.getAll());
companyData.getAll(function (companyDetails) {
console.log(companyDetails)
}
}