cowboydan cowboydan - 14 days ago 6
AngularJS Question

arrow functions and logging json response to console

I call my service and it hits an API and returns a json object. I log the object to console to view the structure of the json object and make sure it returns the data i want.

var promise = getJsonObjectService.getJsonObject();

promise.then(function (data) {
$scope.foo = data;
console.log($scope.foo);
});


I recently learned of Arrow functions and replaced the previous code with this

var promise = getJsonObjectService.getJsonObject();

promise.then(data => $scope.foo = data);


The json object is returned and the page works the same as before, but I cannot figure out how to log the object to console like the previous method. Can you explain how i would log the json object returned in this arrow function? Can you explain maybe a little about what is happening differently under the hood as between these two code blocks?

Answer

This is the way Es6 array function used
promise.then((data) => { $scope.foo = data; console.log($scope.foo); });

function(params ....){} is replaced by (params) => {} to provide outer function 'this' reference to inside function.