Angular 使用get und set方法更改字符串的格式-永远不会调用set方法
我通过http获取日期时间,并希望在使用它之前对其进行格式化。为此,我使用get和set方法,但set方法从未被调用 我的组件(AdminComponent): 管理员服务:Angular 使用get und set方法更改字符串的格式-永远不会调用set方法,angular,typescript,Angular,Typescript,我通过http获取日期时间,并希望在使用它之前对其进行格式化。为此,我使用get和set方法,但set方法从未被调用 我的组件(AdminComponent): 管理员服务: import { Injectable } from '@angular/core'; import { Observable, of } from 'rxjs'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { catchE
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 = ""; //is used but not necessary 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 that failed.
* Let the app continue.
* @param operation - name of the operation that failed
* @param result - optional value to return as the observable result
*/
private handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
// TODO: send the error to remote logging infrastructure
console.error(error); // log to console instead
// Let the app keep running by returning an empty result.
return of(result as T);
};
}
}
我将startdate
的名称更改为\u startdate
,并为startdate
创建了一个get和set方法。在我的模板中,我使用doublebinding对输入字段进行绑定,如:
查看我的控制台,从未调用set方法,但调用get方法。当调用get时,\u startdate未定义。所以我想我做了一些根本错误的事情
我的目标是不必在组件中处理日期形成,我希望直接在Config类中处理
编辑:如果我从AdminService更改函数getConfig()
,分别设置每个变量,请参见此处:
getConfig(): void {
this.adminService.getConfig().subscribe(config => {
this.config.title = config.title;
this.config.startdate = config.startdate;
this.config.enddate = config.enddate;
console.log(this.config);
});
}
由于Config只有3个变量,这不是什么大问题,但是在更大的类中,这并不是我真正想要的。我也不知道为什么第一个版本不起作用?我知道为什么现在,您没有对它进行任何设置。。。当您的代码在中设置值时,将调用SET
getConfig(): void { //THIS IS THE IMPORTANT PART FOR THIS QUESTION
this.adminService.getConfig().subscribe(config => {
this.config = config; // this is where you are calling GET
config.startdate = 'foo'; // this is a SET example
console.log(this.config);
});
}
编辑:如果代码太大,可以在Config类中使用构造函数:
class Config {
private title: string;
private startdate: string;
private enddate: string;
constructor(title: string, startdate: string, enddate: string) {
this.title = title;
this.startdate = startdate;
this.enddate = enddate;
}
// ... getters/setters
}
我认为这是在typescript中使用类的最佳方式。您好,我认为您的问题是在
getConfig
上的subscribe
之后调用该方法。我说的对吗?控制台输出是什么?“被调用”?顺便说一句,我认为new Date().toISOString()
执行的是ParseDateToString的格式doing@Gaspar是,“get Call”是控制台输出,但从不“set called”。同样,toISOString()方法也是正确的。更确切地说,它是:.toISOString().substring(0,16)
-我会在我的问题中编辑它,使代码更小,但它根本不会影响问题。@AlbertoAM tbh请看问题末尾的编辑谢谢,我也发现了同样的问题(请看问题末尾的编辑)。在这个示例中,Config只有3个变量,所以这不是什么大问题。但其他类可能有100个变量。我希望,当我在服务和http请求中使用Config类时,这将自动工作。有更通用的解决方案吗?谢谢!我认为使用构造函数就可以了!我会检查这一点,并接受你的答案,如果它的工作。甚至没有想过使用构造函数..我更仔细地分析了它,发现当我使用this.config=config
this.config的原型changes->getter和setter不再工作,等等。因此将分别设置每个变量,以避免覆盖原型。
getConfig(): void { //THIS IS THE IMPORTANT PART FOR THIS QUESTION
this.adminService.getConfig().subscribe(config => {
this.config = config; // this is where you are calling GET
config.startdate = 'foo'; // this is a SET example
console.log(this.config);
});
}
class Config {
private title: string;
private startdate: string;
private enddate: string;
constructor(title: string, startdate: string, enddate: string) {
this.title = title;
this.startdate = startdate;
this.enddate = enddate;
}
// ... getters/setters
}