Whenever I call a function, I encounter a
Cannot read property of undefined
error message, even though the data is being displayed correctly.
From my understanding so far, this issue arises due to the asynchronous loading of data by AngularJS, and using promises can help prevent it.
I've tried looking online and in Angular's documentation for guidance on using promises, but I couldn't grasp the concept.
If anyone can provide assistance, I would greatly appreciate it.
The current code I have is as follows:
.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;
}
}])
You can find the Plunker link here: http://plnkr.co/edit/3AK70uVbNSdhAIIfdkHb?p=preview