Upon receiving a datetime through an HTTP request, I need to format it before utilizing it. To achieve this, I utilize the get and set methods in my code. However, I noticed that the set method is never invoked.
This is how my component (AdminComponent) looks like:
import { Component, OnInit } from '@angular/core';
import { AdminService } from './admin.service';
import { Config } from './_config';
@Component({
selector: 'app-admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.css']
})
export class AdminComponent implements OnInit {
config: Config = new Config();
constructor(private adminService: AdminService) { }
ngOnInit() {
this.getConfig();
}
getConfig(): void { //THIS IS THE IMPORTANT PART FOR THIS QUESTION
this.adminService.getConfig().subscribe(config => {
this.config = config;
console.log(this.config); //just for debugging
});
}
saveConfig(): void {
//saving logic goes here
}
}
The AdminService implementation:
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { Config } from './_config';
@Injectable({
providedIn: 'root'
})
export class AdminService {
private getConfigUrl = '//../getConfig.php';
private saveConfigUrl = '//../saveConfig.php';
constructor(private http: HttpClient) {
this.getConfigUrl = window.location.protocol+this.getConfigUrl;
this.saveConfigUrl = window.location.protocol+this.saveConfigUrl;
}
getConfig(): Observable<Config> {
var data = ""; //used but not required for this example.
var headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
return this.http.post<Config>(this.getConfigUrl, data, { headers: headers } ).pipe(catchError(this.handleError('admin getConfig', [])));
}
saveConfig(config: Config) {
var data = "config="+JSON.stringify(config);
var headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
return this.http.post<string>(this.saveConfigUrl, data, { headers: headers } ).pipe(catchError(this.handleError('admin saveConfig', [])));
}
/**
* Handle Http operation errors without stopping the app.
* @param operation - name of the failed operation
* @param result - value to return upon failure
*/
private handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
// Send error to logging infrastructure
console.error(error);
// Continue running the app by returning an empty result.
return of(result as T);
};
}
}
Lastly, the Config class which utilizes the get and set methods:
export class Config {
title: string;
_startdate: Date;
enddate: Date;
public get startdate(): string {
console.log("get called");
return this.parseDateToStringWithFormat(this._startdate);
}
public set startdate(v: string) {
console.log("set called");
let actualParsedDate = v ? new Date(v) : new Date();
let normalizedParsedDate = new Date(actualParsedDate.getTime() + (actualParsedDate.getTimezoneOffset() * 60000));
console.log("from: "+v+" to: "+normalizedParsedDate);
this._startdate = normalizedParsedDate;
}
private parseDateToStringWithFormat(date: Date): string {
return date.toISOString().substring(0,16);
}
}
I made changes in the getConfig()
function of AdminService and accessed variables separately, leading to successful execution. Although effective with fewer variables, this approach may not be suitable for larger classes. The reason behind the initial version not working remains unclear.