I'm getting a Cannot read property of undefined error when calling a function, however the data is being displayed correctly.
According to what I've read so far, that's because of asynchronous load of data made by AngularJS and that I should use promises to avoid that.
I've searched online and Angular's documentation but I didn't understand how to use it.
I'd appreciate if someone could help me.
Currently, I have the following code:
.factory('Test', ['$resource', function ($resource) {
return $resource('data.json');
}])
.controller('MyCtrl', ['$scope', 'Test', function($scope, Test) {
Test.get(function (data) {
$scope.myData = data;
});
$scope.myFunction = function() {
var min = $scope.myData.min;
var max = $scope.myData.max;
var diff = max - min;
return diff;
}
}])
Here's a Plunker: http://plnkr.co/edit/3AK70uVbNSdhAIIfdkHb?p=preview