Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Angular.js Factories using $http

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #194301
    rwchampin
    Participant

    Im currently messing around learning Angular. I have a controller thats hooked up to a radial progress bubble, i am creating a factory that uses $http to grab some dummy data from data.json I inject the factory into my controller and set that data into a variable so i can use it inside some other objects. To be exact, i want to take the first element of the returned array from my json file as the dashOffset property on the circle object. When i console.log Data.get() from my controller it prints an empty array…help!!

    var app = angular.module('app',['ui.router'])
        .factory('Data', function($http){
                var data = [];
               return {
                    get : function(){
                        $http.get('data.json').success(function(res){
                            data.push(res);
                            console.log(data)
                            return res;
                        })
    
    `                return data;
                }
    
           }
    
    
    
    
    })
    .controller('radial', function($scope, Data){
    
           console.log(Data.get());
    
        $scope.circle = {
            size : 110,
            stroke : '#2C3E50',
            strokeWidth : 10,
            radius : 48,
            dashArray : Math.PI * 2 * 48,
            dashOffset : (Math.PI * 2 * 48)
        }
        $scope.circleBG = {
            size : 110,
            stroke : '#2C3E50',
            strokeWidth : 10,
            radius : 48,
            dashArray : Math.PI * 2 * 48,
            dashOffset : (Math.PI * 2 * 48)
        }
    
    });
    
    #194325
    jamygolden
    Moderator

    At a guess, I’d say that your data.json file is empty. Try and log out res before you push it to the data array.

    If that’s not the issue, could you re-create your problem on plnkr?

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.