I am working with Angular JS and Yii2. In one of my PHP controllers I need to pass an array of data from backend to front end, so I am json encoding my php array and setting a var in the view like so:
$script = "var projects = '".json_encode($projects)."';";
$this->view->registerJs($script, View::POS_END);
Then in my Angular controller I am getting the JSON data like so:
app.controller('ProjectsController', ['$scope', function($scope, args){
$scope.projects;
$scope.init = function(){
$scope.projects = angular.fromJson(projects);
};
$scope.init();
}]);
My question is, is this good form? Is there a right/wrong way to process data like this on page load with Angular?
Cheers
Ash
$script = "var projects = ".json_encode($projects).";";...$scope.projects = projects;