我写了一个TypeScript模型,我写了一个get方法:
get comments(): [any] {
return this.commentsTest;
}
set comments(comments: [any]) {
this.commentsTest = comments;
}
其自身定义为私有的字段:
private commentsTest: [any];
在构造函数中,我按如下方式初始化了对象:
this.comments = commentsTest;
当我尝试使用get方法使用该字段时,它始终是未定义的, 但是如果我直接访问它(同时获得访问警告) 我确实得到了我想要的值:
openExtendedDetailsModal(comments) {
console.log("***" + this.investorObject.comments); // undefined
console.log(this.investorObject.commentsTest); // defined
//this.commentsdDetails.altOpen(comments);
}
更新 这是我正在使用的代码:
investorObject: Investor;
prametersSubscription: Subscription;
id: string;
constructor(private investorService: InvestorsService, private route: ActivatedRoute) {}
ngOnInit() {
this.prametersSubscription = this.route.params.subscribe(
(params: Params) => {
this.id = params.id;
this.getInvestorData();
}
);
}
ngOnDestroy() {
this.prametersSubscription.unsubscribe();
}
getInvestorData() {
this.investorService.getSingleInvestorById(this.id).subscribe(
data => {
this.investorObject = data.data;
console.log(this.investorObject.commentsTest);
});
}
openExtendedDetailsModal(comments) {
console.log("***" + this.investorObject.lastName); // defined
console.log("***" + this.investorObject.comments); // undefined
console.log(this.investorObject.commentsTest); // defined
//this.commentsdDetails.altOpen(comments);
}
谢谢