I use the factory method in angular.js and $ http.get to capture and process JSON data. The JSON data seems to have been successfully parsed in the factory, but I have the property of accessing the problem of this JSON data.
Here is my js code:
var app = angular.module("app", []); app.factory('mainInfo', function($http) { var obj = {content:null}; //the php will return json data below $http.get('http://localhost/test.php').success(function(response){ obj.content = response.records; }); return obj; }); app.controller('Ctrl', function($scope, mainInfo){ $scope.foo = mainInfo.content; }) ;
Now, if I try to access foo inside the Ctrl controller, the web page will not display data:
<div ng-controller="Ctrl">Controller: {{foo}}</div>
However, if I change to $scope.foo = mainInfo in Ctrl , the web page will correctly display JSON data.
Can I find out what is the correct way to access the mainInfo.content property in the Ctrl controller?
The reason I need to access the JSON property is because I need to pre-process the data. I intend to use this data in the diagram, as in the lower controller. Currently, this controller also does not work, because I have the same problem with accessing the JSON property as in the Ctrl controller.
app.controller("LineCtrl", function ($scope, mainInfo) { var timePoints = []; var percentagePoints = []; var i = 0; for( i = 0; i < mainInfo.content.length; i ++) { timePoints.push(mainInfo.content[i].Time); percentagePoints.push(mainInfo.content[i].Percentage); } $scope.labels = timePoints; $scope.data = percentagePoints; $scope.onClick = function (points, evt) { console.log(points, evt); }; });
Json data:
{ "records": [ { "Id": "1", "Time": "2015-07-25 08:00:00", "Percentage": "60" }, { "Id": "2", "Time": "2015-07-25 09:00:00", "Percentage": "70" }, { "Id": "3", "Time": "2015-07-25 10:00:00", "Percentage": "80" } ] }
Regarding factory -controller communication, I just refer to the solution from another post: link