ionic 2 how to load dynamically with index

2019-03-06 02:18发布

For ionic 1 i have done like ng-if="$index % 3 === 0" But i need to load the data dynamically in grid view with two col in one row. How can i do that. i tried below code :

in my schudle.ts :

ResourceData = [{ name: "ksjs" },{ name: "daa" },{ name: "das" }, {name: "das" }, {name: "Computer Science" }, { name: "Moc" }]

My html :

 <div class="item item-body no-padding" style="border-width: 0px !important; overflow-y:hidden;height: 65%;">

                <div class="row no-padding" style="height: 65%;" *ngFor="let data of ResourceData" *ngIf="index % 2 == 0">
                    <div class="col col-50 custom-design2" style="background: url(url) no-repeat center;background-size: cover;">
                        <div class="custom-design1"><span class="grid-title">{{data[index].name}}</span></div>
                    </div>
                    <div class="col col-50 custom-design2" style="background: url(url) no-repeat center;background-size: cover;">
                        <div class="custom-design1"><span class="grid-title">{{data[index + 1].name}}</span></div>
                    </div>
                </div>
</div

But it din work . May be that ngif may be wrong. please tell me how can i solve this.

i need like this :

as      fa

fas     faaf

faf      faf

fafa     fafaf

So two col per row. Please tell me what i am doing wrong !!

Thanks

ionic 1

 <div class="row no-padding" ng-repeat="mydash in Data"  ng-if="$index % 3 === 0">
                <div class="col col-30 custom-design2" style="background: url({{Data[$index].Image}}) no-repeat center;background-size: cover;" ng-click="changestate(Data[$index])" ng-if="$index < Data.length">
                    <div class="custom-design1"><span class="grid-title">{{Data[$index].name}}</span></div>
                </div>
                <div class="col col-30 custom-design2" style="background: url({{Data[$index + 1].Image}}) no-repeat center;background-size: cover; " ng-click="changestate(Data[$index + 1])" ng-if="$index + 1 < Data.length">
                    <div class="custom-design1"><span class="grid-title">{{Data[$index + 1].name}}</span></div>
                </div>

                <div class="col col-30 custom-design2" style="background: url({{Data[$index + 2].Image}}) no-repeat center;background-size: cover; " ng-click="changestate(Data[$index + 2])" ng-if="$index + 2 < Data.length">
                    <div class="custom-design1"><span class="grid-title">{{Data[$index + 2].name}}</span></div>
                </div>
            </div> 

1条回答
劫难
2楼-- · 2019-03-06 02:28

You are using two structural directives in the same tag.Both *ngFor and *ngIf in one div.

There's an easy solution for this use case: put the *ngIf on a container element that wraps the *ngFor element. One or both elements can be an ng-container so you don't have to introduce extra levels of HTML.

Use ng-container

You can also get index by appending let i =index in *ngFor.

<div class="row no-padding" style="height: 65%;" *ngFor="let data of ResourceData;let i = index">
     <ng-container *ngIf=" i % 2 == 0">
          <div class="col col-50 custom-design2" style="background: url(url) no-repeat center;background-size: cover;">
               <div class="custom-design1"><span class="grid-title">{{ResourceData[i].name}}</span></div>
            </div>
            <div class="col col-50 custom-design2" style="background: url(url) no-repeat center;background-size: cover;">
                 <div class="custom-design1"><span class="grid-title">{{ResourceData[i+1].name}}</span></div>
             </div>
     </ng-container>
</div>
查看更多
登录 后发表回答