简体   繁体   English

显示Nativescript ListView中的项目

[英]Display items in the Nativescript ListView

I am learning Nativescript with Angular 2 and trying to get Random user data from https://randomuser.me API. 我正在使用Angular 2学习Nativescript并尝试从https://randomuser.me API获取随机用户数据。 I am not able to display just the names of 10 random users in the ListView. 我无法在ListView中仅显示10个随机用户的名称。 It only displays 10 blank List Items in the ListView. 它仅在ListView中显示10个空白列表项。

Here is my app.component.ts 这是我的app.component.ts

import {Component} from "@angular/core";
import {Http, HTTP_PROVIDERS, Response} from "@angular/http";
import {Observable} from "rxjs/Rx";

@Component({
    selector: "my-app",
    templateUrl: "app.component.html",
    providers: [HTTP_PROVIDERS]
})
export class AppComponent {
    public names: Observable<Array<Object>>;

constructor(public http: Http){

}

public onTap(){
    this.getData()
    .subscribe(
        (res) => {
            this.names = res.json().results;
        }
    );
}

public getData(){
    return this.http.get('https://randomuser.me/api/?results=10');

}

} }

And here is my html template file 这是我的html模板文件

<StackLayout>
    <Button text="GET" (tap)="onTap()"></Button>
    <ListView [items]="names">
        <template let-item="item">
            <Label (text)="item.gender" textWrap="true"></Label>        
        </template>
    </ListView>
</StackLayout>

Any help is highly appreciated. 任何帮助都非常感谢。

<Label (text)="item.gender" textWrap="true"></Label>更改为<Label [text]="item.gender" textWrap="true"></Label>

It looks like you have made a mistake in the way you have added the binding for the text property in the ListView item Template . 看起来你在ListView项Template添加了text属性的绑定方式时出错了。 Simply change the (text)="item.gender" to [text]="item.gender" . 只需将(text)="item.gender"更改为[text]="item.gender"

Note that for " One-way from data source to view target " in angular 2 you should use the [] syntax, the () is an " One-way from view target to data source " and for example could be used for event handlers. 请注意,对于角度为2的“ 从数据源到视图目标的单向 ”,您应该使用[]语法, ()是“ 从视图目标到数据源的单向 ”,例如可以用于事件处理程序。

Here you can find more details about angular binding syntax. 在这里您可以找到有关角度绑定语法的更多详细信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM