My challenge is with a service that is supposed to store specific data loaded from a JSON file and display it on the webpage immediately upon receiving. I attempted to set a $scope variable equal to the data in the service, but the data did not show up right away.
I found success only when using:
angular.copy(response.data, this.animals)
,
however, I am puzzled as to why it doesn't work when I use: this.animals = response.data
. I am curious to understand why this discrepancy exists.
module.service("animalService", function($http) {
this.animals=[];
$http.get('animals.json').then(function(response) {
//this.animals = response.data ----> not working
angular.copy(response.data, this.animals)
});
});
module.controller("animalController", function($scope, animalService) {
//$scope.animals is used to populate an HTML table
$scope.animals = animalService.aniamsl;
});