angular2 템플릿에서 typescript 속성을 호출하고 싶습니다. 루프의 객체 속성을 호출하면 그렇게 할 수 없습니다. 루프가 없으면 코드는 정상적으로 작동합니다.angular2에서 typescript getter 호출
Calling method from a Angular 2 class inside template이 질문은 루프가없는 경우 문제를 해결합니다.
Plunker here을 찾으십시오.
import {Component} from '@angular/core'
export class User {
id: number;
mail: string;
created_at: string;
first_name: string;
last_name: string;
deleted_at: any;
// if I call {{user.name}} nothing gets printed.
get name() {
return "My name is:" + this.first_name;
}
}
@Component({
selector: 'my-app',
template: `
<ion-list *ngFor="let user of users">
<div>{{user.first_name}}</div>
</ion-list>
`
})
export class App {
users: User[];
constructor() {
this.users = [ {first_name:"Jagan"}, {first_name:"Nath"} ];
}
}
업데이트 여기
코드의 조각이다. 작동 버전과 작동 버전이 아래에 나와 있습니다.
//our root app component
import {Component, Input} from '@angular/core'
import { MyComponent } from './my-component';
export class User {
first_name : string;
get name() {
return "Name:" + this.first_name;
}
}
@Component({
selector: 'my-app',
template: `
<div>
{{user.first_name}}
</div>
`,
directives: [ MyComponent ]
})
export class App {
constructor() {
let str = "{ \"first_name\" : \"Jagan\" }";
this.user = JSON.parse(str) as User;
}
}
// 이것은 작동하지 않습니다.
/* @Component({
selector: 'my-app',
template: `
<div>
{{user.name}}
</div>
`,
directives: [ MyComponent ]
})
*/
죄송하지만 업데이트를 읽은 것으로 간주합니다. – Jagannath
아니, 나는 그것을 놓쳤다. 그러나 나는 지금 대답을 업데이트했다. 아이디어는 여전히 동일합니다. 사용자의 생성자를 사용하십시오. – Sjoerd
이 기능이 작동합니다. 나는 plunker 샘플을 테스트했다. 그것은 거기서 일했습니다. 대부분 내 응용 프로그램에서 작동합니다. 집에 가면 시험해볼거야. 당분간 나는 대답을 받아 들였다. 나는 지금 그 문제를 이해했다. – Jagannath