【发布时间】:2016-06-24 15:54:52
【问题描述】:
我试图在模板中创建一个 if 语句,以在数组长度为
我就是这样尝试的:
<div *ngIf="socialNetworks.length > 1">
<div class="alert-box warning radius">You can still connect some accounts</div>
<button class="btn btn-primary" *ngFor="let network of socialNetworks" (click)="loginSocialNetwork(network)">
{{network.title}}
</button>
</div>
但我总是收到一个错误,说它无法读取未定义的属性长度。
我在 Angular 2 组件中定义了变量 socialNetworks[]:
export class MyAccountComponent implements OnInit {
socialNetworks: socialNetwork[];
campaigns: Campaign[];
showGreeting: boolean = true;
constructor(
private _dataservice: DataService) {
}
然后,在一个单独的方法中,我在此处从金字塔视图的响应中设置值:
getSocialNetworks() {
var url: string;
url = "/account_api"
this._dataservice.getDataByUrl(url)
.subscribe(
res => this.socialNetworks = res,
err => this.logError(err)
)
}
即使我在此处的末尾添加一个 console.log 语句来查看 this.socialNetworks 的值,它也会说它是未定义的。但是在调试器中我可以看到 this.socialNetworks 的值不是未定义的。
所以我的问题是,我只是错误地引用了全局变量,还是我错过/误解了一些东西?
【问题讨论】:
标签: debugging typescript angular global-variables