简体   繁体   English

在 repeat.for 如何索引数组中的第一项

[英]in repeat.for how to index the first item in an array

I have a template that i want to load for the length of my array.So when i load the page first i want it to load the first item in the array and then when next is clicked it loads the next item.我有一个模板,我想为我的数组的长度加载。所以当我首先加载页面时,我希望它加载数组中的第一项,然后在单击下一步时加载下一项。

this is my html这是我的html

<div repeat.for="item of items">
            <div class="row">
                <div class="center">
                    <div>
                        <md-label > ${item.name}</md-label><br /><br />
                        <md-label> ${user.firstName},</md-label><br />
                         <md-label> ${item.Description}</md-label><br />
                        
                    </div>
                </div>

            </div>
            <div class="row">
                
                <div class="col">

                    <md-radio name="myOption" value="0" checked.bind="choice">Never</md-radio><br />
                
                </div>
                <div><a click.delegate="next()">next</a></div>
            </div>
        </div>

async onLoad() {
        this.items= await this.query<ItemQuery, ItemModel>(new ItemQuery({ id: this.id }));
        this.user= await this.query<fetchUser, UserModel>(new fetchUser(this.UserId));
        
        
    }
async next(){}

currently its repeating every item in my items array on load.目前它在加载时重复我的项目数组中的每个项目。

an example of my array is我的数组的一个例子是

this.items=[{name:"Test1",description:"this is test 1"}]
[{name:"Test2",description:"this is test 2"}]
[{name:"Test3",description:"this is test 3"}]

im not sure how to load on first instance only the values for Test1 and then when next is clicked fetch Test2 details using the above html?我不确定如何仅在第一个实例上加载 Test1 的值,然后在单击下一步时使用上述 html 获取 Test2 详细信息?

Since only one item is being displayed here, you can index into the items array, and increment the index when next is clicked.由于此处仅显示一项,因此您可以对 items 数组进行索引,并在单击next时增加索引。

<div>
    <div class="row">
        <div class="center">
            <div>

                <md-label> ${items[index].name}</md-label><br /><br />
                <md-label> ${user.firstName},</md-label><br />
                <md-label> ${items[index].Description}</md-label><br />

            </div>
        </div>

    </div>
    <div class="row">

        <div class="col">

            <md-radio name="myOption" value="0" checked.bind="choice">Never</md-radio><br />

        </div>
        <div><a click.delegate="next()">next</a></div>
    </div>
</div>

async onLoad() {
    this.index = 0;
    this.items = await this.query < ItemQuery, ItemModel > (new ItemQuery({ id: this.id }));
    this.user = await this.query < fetchUser, UserModel > (new fetchUser(this.UserId));
}

async next(){
    this.index = (this.index + 1) % this.items.length;
}

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

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