Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

Ok here we go. I have an angularjs platform and I want to call the data in my controllers scope from an external php files. This is the original code.

.controller("morrisChartCtrl", ["$scope",
        function ($scope) {
            return $scope.mainData = [{
                month: "2013-01",
                xbox: 294e3,
                will: 136e3,
                playstation: 244e3
            }, {
                month: "2013-02",
                xbox: 228e3,
                will: 335e3,
                playstation: 127e3
            }, {
                month: "2013-03",
                xbox: 199e3,
                will: 159e3,
                playstation: 13e4
            }, {
                month: "2013-04",
                xbox: 174e3,
                will: 16e4,
                playstation: 82e3
            }, {
                month: "2013-05",
                xbox: 255e3,
                will: 318e3,
                playstation: 82e3
            }, {
                month: "2013-06",
                xbox: 298400,
                will: 401800,
                playstation: 98600
            }, {
                month: "2013-07",
                xbox: 37e4,
                will: 225e3,
                playstation: 159e3
            }, {
                month: "2013-08",
                xbox: 376700,
                will: 303600,
                playstation: 13e4
            }, {
                month: "2013-09",
                xbox: 527800,
                will: 301e3,
                playstation: 119400
            }], $scope.simpleData = [{
                year: "2008",
                value: 20
            }, {
                year: "2009",
                value: 10
            }, {
                year: "2010",
                value: 5
            }, {
                year: "2011",
                value: 5
            }, {
                year: "2012",
                value: 20
            }, {
                year: "2013",
                value: 19
            }], $scope.comboData = [{
                year: "2008",
                a: 20,
                b: 16,
                c: 12
            }, {
                year: "2009",
                a: 10,
                b: 22,
                c: 30
            }, {
                year: "2010",
                a: 5,
                b: 14,
                c: 20
            }, {
                year: "2011",
                a: 5,
                b: 12,
                c: 19
            }, {
                year: "2012",
                a: 20,
                b: 19,
                c: 13
            }, {
                year: "2013",
                a: 28,
                b: 22,
                c: 20
            }], $scope.donutData = [{
                label: "Download Sales",
                value: 12
            }, {
                label: "In-Store Sales",
                value: 30
            }, {
                label: "Mail-Order Sales",
                value: 20
            }, {
                label: "Online Sales",
                value: 19
            }]
        }
    ])

Instead of inline data in the js file I want to use $http to get the code for each scope from a individual php file that will generate to JSON format.

What is wrong with this format

.controller("morrisChartCtrl", ["$scope",
        function ($scope) {
            return $scope.mainData = $http.get('php/mainData.php').success(function (data) {
    $scope.mainData = data;
}), $scope.simpleData = $http.get('php/simpleData.php').success(function (data) {
    $scope.simpleData = data;
}), $scope.comboData = $http.get('php/comboData.php').success(function (data) {
    $scope.comboData = data;
}), $scope.donutData = $http.get('php/donutData.php').success(function (data) {
    $scope.donutData = data;
})
        }
    ])

Just trying to figure out the right structure

share|improve this question
    
    
what is the question? You already know about $http, read the docs , go through the docs tutorial code that shows examples...do some research in other words. – charlietfl Jul 20 '14 at 20:20
    
I guess what I am asking is if I have the inline code there what would be the structure of creating $http driven version something like the following – Univelocity Jul 20 '14 at 20:44

1 Answer 1

What you get as a result of $http.get() is a promise object. Avoid assigning the result of $http.get() to a scope variable.

.controller("morrisChartCtrl", ["$scope",
  function ($scope) {
    var mainDataPromise = $http.get('php/mainData.php');
    mainDataPromise.success(function (data) {
      $scope.mainData = data;
    }); 

    var simpleDataPromise = $http.get('php/simpleData.php');
    simpleDataPromise.success(function (data) {
      $scope.simpleData = data;
    });

    var comboDataPromise = $http.get('php/comboData.php');
    comboDataPromise.success(function (data) {
      $scope.comboData = data;
    })

    var donutDataPromise = $http.get('php/donutData.php');
    donutDataPromise.success(function (data) {
      $scope.donutData = data;
    });
  }
])
share|improve this answer
    
When I replace the code I loose all data either it is not returning the data or the data is not formatted correctly and I am trying to figure that out. i literally took the data displayed and put it into a php file and used echo ' the data '; not sure if that is correct or even if the file path is the issue – Univelocity Jul 22 '14 at 17:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.