After creating an angular controller and service to extract data from a JSON file, everything seemed to be working smoothly. However, I encountered an issue when attempting to assign this data to a new object.
My goal is to implement a new function within my promise that accepts the data as an object, allowing me to utilize it in various parts of the application. Here is the code for the controller:
class WPEntryController {
static $inject = ["$location", "WPEntryService"];
constructor($location, WPEntryService, $http) {
console.log("IN WPEntryController");
this.$location = $location;
this.WPEntryService = WPEntryService;
this.loadWPEntryPagewithData();
}
loadWPEntryPagewithData(){
this.WPEntryService.loadWPEntryData().then(function(promise){
this.DataObject = promise;
this.storeObject();
});
}
storeObject() {
console.log(this.DataObject);
}
}
angular.module("app").controller("WPEntryController", WPEntryController);
Here is the service code:
class WPEntryService {
static $inject = ["$http"];
constructor($http) {
this.$http = $http;
}
loadWPEntryData() {
// read json file or provide URL for data
var promise = this.$http.get('...')
.then(function (dataObject) {
return dataObject.data;
})
.catch(function (response) {
return response;
});
return promise;
}
}
angular.module('app').service('WPEntryService',WPEntryService);