简体   繁体   English

如何在angular2中使用* ngFor绑定引导模式

[英]How to bind bootstrap modal using *ngFor in angular2

I want to display my data in the bootstrap modal using angular2. 我想使用angular2在引导程序模式中显示我的数据。 I have binded my data in modal using '*ngFor'. 我已经使用'* ngFor'将数据绑定到模式中。 But using *ngFor, modal not showing data as per their behaviour(when clicking on the right arrow, data should change and first data come as default). 但是使用* ngFor时,模式不会按照其行为显示数据(单击右箭头时,数据应更改,并且第一个数据为默认值)。 but when I apply active class with the 'item', its showing complete data but in vertically format. 但是当我将活动类与“项目”一起应用时,它会显示完整的数据,但格式为垂直。 And I want it should show one image and its detail in one's. 我希望它能显示一张图像及其细节。

Here is my 这是我的

app.component.html app.component.html

<div class="modal fade" id="feedbackModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" title="Close"> <span class="glyphicon glyphicon-remove"></span></button>
            </div>
            <div class="modal-body">
                <div class="row text-center">
                    <h4>Details</h4>
                </div>

                <div id="myGallery" class="carousel slide" data-interval="false">
                    <div class="carousel-inner">
                        <div class="item active" *ngFor="let modalData of cnnTableData">
                            <div class="row">
                                <div class="col-sm-6">
                                    <br>
                                    <img src={{modalData.Image}} style="width:100%;min-height: 211px;">
                                </div>
                                <div class="col-sm-6">
                                    <div>
                                        <h6><b>Image Name</b></h6>
                                        <small>{{modalData.Name}}</small>
                                    </div><br>
                                </div>
                            </div>
                        </div>
                    </div>
                    <a class="left carousel-control" href="#myGallery" role="button" data-slide="prev" style="margin-left: -84px;"> <span class="glyphicon glyphicon-chevron-left"></span></a>
                    <a class="right carousel-control" href="#myGallery" role="button" data-slide="next" style="margin-right: -84px;"> <span class="glyphicon glyphicon-chevron-right"></span></a>

                </div>
            </div>
            <div class="modal-footer">
                <br><br>
            </div>
        </div>
    </div>
</div>

And my output which is coming from above code : 和我的输出是从上面的代码: 在此处输入图片说明

at first you need to organize the application, divide your application into components . 首先,您需要组织应用程序,将应用程序分为多个组件

for example create a component represent the HTML template where *ngFor directive is mentioned in you application 例如,创建一个表示HTML模板的组件,其中在应用程序中提到了* ngFor指令

the component TypeScript file 组件TypeScript文件

@Component({
        selector: 'my-component',
        templateUrl: 'my-component.html', // mentioned below
      })
      export class myComponent implements OnChanges, OnInit{
        @Input index:number;
        @Input selectedIndex:number;

        className:string;

       ngOnInit(){
         this.className = index==0?"active":"";
       }

       ngOnChanges(){
         if(selectedIndex == index)
           this.className = "active";
         else
           this.className = "";
       }
      }



and this is the component template, HTML file 这是组件模板HTML文件

<div [class]="'item' className">
   <div class="row">
      <div class="col-sm-6">
          <br>
              <img src={{modalData.Image}} style="width:100%;min-height: 211px;">
     </div>
     <div class="col-sm-6">
          <div>
             <h6><b>Image Name</b></h6>
             <small>{{modalData.Name}}</small>
          </div><br>
     </div>
 </div>




now we have the component, lets use it in the appComponent. 现在我们有了组件,可以在appComponent中使用它。

inside the appComponent 在appComponent内部

  <my-component *ngFor=" let modalData of cnnTableData ;#i = index" [index]="i" [selectedIndex]="currentIndex"> //current index is in the appComponent class </my-component> 


inside the appComponent class you need to track the click event and reflect it to the variable currentIndex 在appComponent类中,您需要跟踪click事件并将其反映到变量currentIndex

 currentIndex:number = 0; clickLeft(){ this.currentIndex++; } clickRight(){ this.currentIndex--; } // take in account the length of the items. 

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

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