I Have the following code in which I'm trying to get a JSON file and attach its contents to my $scope.
The first console.log returns the result that I need, but the second returns undefined.
What would be the correct way to write this so that the data is stored?
'use strict';
var myApp = angular.module('myApp.view1', ['ngRoute']);
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {
templateUrl: 'view1/view1.html',
controller: 'View1Ctrl'
});
}]);
myApp.controller('View1Ctrl', [
'$scope',
'$http',
function($scope, $http) {
$http.get('view1/info.json')
.then(function(res){
$scope.data = res.data.info
console.log($scope.data)
});
console.log($scope.data)
}]);
console.log()runs because$httpis asynchronous and takes time to complete. What exactly are you wanting to do.