【问题标题】:dataItems array how to display in view like JSONdataItems 数组如何像 JSON 一样在视图中显示
【发布时间】:2019-07-11 15:17:47
【问题描述】:

我对我的代码有疑问。

从这段代码中,我在控制台中显示了这个数组:

 public country: Country[] = [];
  public _items: TokenModel[] = [];
   @ViewChild("autocomplete") autocomplete: RadAutoCompleteTextViewComponent;

   ngOnInit(): void {
        this.getallcountry();
    }
    getallcountry() {
        this.ws.getAllCountryws().subscribe(
            country => {
                this.country = country;
                const mycountry = country;
                console.log('mycountry', mycountry) // show correct JSON
                 for (let i = 0; i < mycountry.length; i++) {
                console.log(mycountry.length) // show correct
                     this._items.push(new TokenModel(mycountry[i].company_id, null));
                }
            },
            err => console.error('err', err),
            () => console.log('error')
        );
    }
get dataItems(): TokenModel[] {
    console.log('this._items', this._items)
    return this._items;
}

在控制台中显示:

JS: this._items [Afghanistan, Albania, Algeria, American Samoa, Andorra, Angola, Anguilla, Antarctica, Antigua and Barbuda, Argentina, Armenia, Aruba, Australia, Austria, Azerbaijan, Bahamas, Bahrain, Bangladesh, Barbados, Belarus, Belgium, Belize, Benin, Bermuda, Bhutan, Bolivia, Bonaire, Bosnia and Herzegovina, Botswana, Bouvet Island, Brazil, British Indian Ocean Territory, Brunei Darussalam, Bulgaria, Burkina Faso, Burundi, Cambodia, Cameroon, Canada, Cape Verde, Cayman Islands, Central African Republic, Chad, Chile, China, Christmas Island, Cocos (Keeling) Islands, Colombia, Comoros, Congo, Democratic Republic of the Congo, Cook Islands, Costa Rica, Croatia, Cuba, Curaçao, Cyprus, Czech Republic, Cote d'Ivoire, Denmark, Djibouti, Dominica, Dominican Republic, Ecuador, Egypt, El Salvador, Equatorial Guinea, Eritrea, Estonia, Ethiopia, Falkland Islands (Malvinas), Faroe Islands, Fiji, Finland, France, French Guiana, French Polynesia, French Southern Territories, Gabon, Gambia, Georgia, Germany, Ghana, Gibra...

从 html 我想在自动完成中显示并编写以下代码:

<RadAutoCompleteTextView #autocomplete [items]="dataItems" suggestMode="Suggest"
                displayMode="Tokens" row="1" col='0' hint="Country">
                <SuggestionView tkAutoCompleteSuggestionView>
                    <ng-template tkSuggestionItemTemplate let-country="item">
                        <StackLayout orientation="vertical" padding="10">
                            <Label [text]="text"></Label>
                        </StackLayout>
                    </ng-template>
                </SuggestionView>
            </RadAutoCompleteTextView>

在视图中不显示任何内容。我认为问题在于结果在数组中,而不是在 JSON 中。

您能建议我如何在视图国家/地区显示吗?

【问题讨论】:

标签: angular autocomplete nativescript


【解决方案1】:

您正在使用默认的 TokenModel,其中唯一有效的属性是 textimage。但是您试图在您的模板中绑定name,这将是未定义的。

此外,您似乎必须在 SuggestionView 中使用 ObservableArray。

Updated Playground

【讨论】:

    【解决方案2】:

    你应该使用 Observables 因为这里是更新的代码

        public _items: ObservableArray<TokenModel>;
    
    constructor(private service: ItemService) {
        this.getallcountry();
    }
    
    ngOnInit(): void {
    
    }
    getallcountry() {
        this._items = new ObservableArray<TokenModel>();
        let countries = this.service.getItems();
        for (let i = 0; i < countries.length; i++) {
            this._items.push(new TokenModel(countries[i].name, undefined));
        }
    }
    get dataItems(): ObservableArray<TokenModel> {
        //console.log('this._items', this._items)
        return this._items;
    }
    

    也在你的html中

    <StackLayout backgroundColor="#66cdaa" padding="5">
    <Label text="Select country"></Label>
    <RadAutoCompleteTextView [items]="dataItems" suggestMode="Suggest"
        displayMode="Tokens">
        <SuggestionView tkAutoCompleteSuggestionView>
            <ng-template tkSuggestionItemTemplate let-item="item">
                <StackLayout orientation="vertical" padding="10">
                    <Label [text]="item.text"></Label>
                </StackLayout>
            </ng-template>
        </SuggestionView>
    </RadAutoCompleteTextView>
    

    这应该可行。

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多