1

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

3
  • tutorials.jenkov.com/angularjs/ajax.html Commented Jul 20, 2014 at 20:18
  • 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. Commented Jul 20, 2014 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 Commented Jul 20, 2014 at 20:44

1 Answer 1

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;
    });
  }
])
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.