I have written angular controller and service to read data from json file. service read data and passes it to controller it works fine but when I try to assign this data to new object it wont work.
I want to call a new function in my promise and pass my data as object to this new function so I can use it whereever required. controller code,
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);
services 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);