html 구성에서 비동기 개체를 사용하는 데 어려움을 겪고 있습니다. 다음과 같이정의되지 않은 angular2의 속성 'version'을 읽을 수 없습니다
export class Version {
isGood: boolean;
constructor(isGood: boolean) {
this.isGood= isGood;
}
}
이 모델은 구성 요소에 의해 호출됩니다 : 버전 변수에
@Injectable()
export class MyComponent {
public version: Version;
constructor(private _myService: VersionService) {}
getVersion(): void {
// async service that gets the versions
this._myService.getVersion().subscribe(
data => this.version= data,
error=> console.log(error),
() => console.log("getting all items complete")
);
}
}
내 템플릿의 참조를 다음과 같이 여기
내 모델입니다<button (click)="getVersion()">Get Version</button>
<hr>
<p style="color:red">{{error}}</p>
<h1>Version</h1>
<p>{{version.isGood}}</p>
그러나 예외가 발생합니다.
Cannot read property 'isGood' of undefined
인터넷을 청소하는 것으로부터 버전 객체가 null이기 때문에 내 문제가있는 것으로 봅니다. 내가 좋아하는 뭔가를 할 경우
<p>{{version | json}}</p>
를 나는 내가 아무것도
나는를 MyComponent 편집하는 경우를 볼
<p>{{version.isGood | async}}</p>
과 같이 할 경우 올바른 버전
을보고,
을 설정할 수 있습니다public version: Version = new Version();
.isGood 속성 가져 오기를 실행할 수는 있지만 항상 비어 있습니다.
비동기 방식으로 사용하는 경우 다른 방법으로 속성을로드해야합니까?
미안하지만 어떻게 관련이 있는지 모르겠다. TS 구성 요소가 아니라 서비스를 직접 사용하고 있습니다. – angryip
구성 요소 시스템을 수정하려고했습니다. @Comjective가 아닌 @Component를 생성하기 위해 @Injectable()을 사용하고 있습니다. –
아. 당신이 올바른지. 나는 지금 그것을 바꿀 것이다, 내가 얼마나 멀리 도착하는지 보아라. – angryip