gpt4 book ai didi

javascript - Angular 2 : access method of a class in template

转载 作者:行者123 更新时间:2023-12-03 05:38:45 27 4
gpt4 key购买 nike

我在 Angular2 应用程序中有以下类(class)

export class Contact {

constructor(
public has_reply: boolean,
public archived: boolean
) { }

getStatus() : string {
if (this.archived) {
return "Archived";
}

if (this.has_reply) {
return "Answered";
}

return "Waiting";
}
}

由服务返回

@Injectable()
export class ContactsService {

private contactsData : BehaviorSubject<Array<Contact>> = null;

constructor(private http: Http) {
this.contactsData = new BehaviorSubject<Array<Contact>>([]);
}

/**
* get the list of contacts
*/
populateContacts() : Array<Contact> {
return this.http.get('/api/contacts/').map(
(res: Response) => {return res.json()}
).subscribe(
jsonData => {
this.contactsData.next(<Array<Contact>> jsonData);
}
);
}

onContactsChanged() : Observable<Array<Contact>>{
return this.contactsData.asObservable();
}

}

在组件中使用

@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

private contacts: Array<Contact> = [];

constructor(
private contactsApi : ContactsService
) { }

ngOnInit() {
this.contactsApi.onContactsChanged().subscribe(
(contacts: Array<Contact>) => {this.contacts = contacts;}
);
this.contactsApi.populateContacts();
}

}

并显示在模板中

<table class="table table-striped table-bordered">
<tr *ngFor="let contact of contacts">
<td>
{{ contact.getStatus() }}
</td>
</tr>

我收到以下错误

EXCEPTION: Error in ./HomeComponent class HomeComponent - inline
template:11:8 caused by: self.context.$implicit.getStatus is not a function

我的方法有什么问题吗? Angular2 允许调用这样的类方法吗?

注:Calling method from a Angular 2 class inside template看起来类似的问题,但没有帮助

最佳答案

正如@AdnanA 所建议的,问题是选 Angular 问题。请参阅How to do runtime type casting in TypeScript?

我通过强制转换数组的每个对象来修复:请参阅 https://stackoverflow.com/a/32186367/117092

// Thank you! https://stackoverflow.com/a/32186367/117092
function cast<T>(obj, cl): T {
obj.__proto__ = cl.prototype;
return obj;
}

@Injectable()
export class ContactsService {

private contactsData : BehaviorSubject<Array<Contact>> = null;

constructor(private http: Http) {
this.contactsData = new BehaviorSubject<Array<Contact>>([]);
}

/**
* get the list of contacts
*/
populateContacts() : Array<Contact> {
return this.http.get('/api/contacts/').map(
(res: Response) => {return res.json()}
).subscribe(
jsonData => {
// WRONG! this.contactsData.next(<Array<Contact>> jsonData);
// FIXED BY
let contactsArray: Array<Contact> = [];
for (let i=0, l=jsonData.length; i<l; i++) {
let contact = cast<Contact>(jsonData[i], Contact);
contactsArray.push(contact);
}
this.contactsData.next(contactsArray);
}
);
}

onContactsChanged() : Observable<Array<Contact>>{
return this.contactsData.asObservable();
}

}

关于javascript - Angular 2 : access method of a class in template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40629688/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com