I have been trying to use data from my service for a chart in my angularjs controller.I have made a service in service.js which i am using in my controller.All i get is the Object array,but when i try to access values inside the Object array ,it throws errors for each approach i use .Following is what i am doing
My Service
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);
});
this.getPlayers=function()
{
return deferred.promise;
}
})
My Controller
angular.module("app.controllers", [])
.controller("gaugeCtrl", ["$scope","config","myService",
function ($scope,config,myService) {
var promise=myService.getPlayers();
promise.then(function(data)
{
$scope.players=data.data;
//console.log($scope.players.Tweets);
($scope.players.Tweets).each(function(index, element) {
console.log(element.FAVOURITE_COUNT);
});
});
return $scope.gaugeHome = {
gaugeData: {
maxValue: 9000,
animationSpeed: 100,
val: 1000
},
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]
]
}
}
I have also included the service Module in my app.js :
var app = angular.module("app",["demoService"])
How can i access a particular value for my gaugectrl properly .Is following a wrong way to access it ?
($scope.players.Tweets).each(function(index, element) {
console.log(element.FAVOURITE_COUNT);
Thanks in Advance
Looks like everything is working. It seems like you are trying to access the objects in the array incorrectly.
I think you want to use Array.forEach
someArray.forEach(function(element, index, array) {
// Do stuff here.
});
I made a simplified version of your code:
var app = angular.module('app', []); app.controller('myController', function($scope, $q, $http) { var deferred = $q.defer(); $http.get('http://enlytica.com/RSLivee/rest/census').then(function(data) { deferred.resolve(data); }); var promise = deferred.promise; promise.then(function(data) { var tweets = []; var total = 0; $scope.players = data.data; ($scope.players.Tweets).forEach(function(element) { console.log(element); tweets.push({FAVOURITE_COUNT: element. FAVOURITE_COUNT, TWEET_ID: element.TWEET_ID}); total += element.FAVOURITE_COUNT; }); $scope.something.tweets = JSON.stringify(tweets, null, 2); $scope.something.favoriteTotal = total; }); $scope.something = {}; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> <div ng-app='app' ng-controller='myController'> {{ something.favoriteTotal }} <pre>{{ something.tweets }}</pre> </div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.