简体   繁体   English

如何在angular5中循环遍历ngx-smart-modal

[英]How to loop through ngx-smart-modal in angular5

I'm working with ngx-smart-modal in angular 5 project, ngx-smart-modal works perfect when not inside the *ngFor loop. 我在angular 5项目中使用ngx-smart-modal,当不在* ngFor循环中时,ngx-smart-modal可以完美工作。 All I want is to fetch data danymically from database and display accordinng to number of loop. 我想要的只是从数据库中以抽象方式获取数据并根据循环数显示。 here is my code. 这是我的代码。

<div class="container">

    <div class="row">

     <div class="col-md-4 col-sm-6" *ngFor = "let modules of get_modules; let i = index">
          <article class="post-news"><a class="mouserPointer" (click)="ngxSmartModalService.getModal('myModal').open()"><img src="./assets/images/{{ modules.imagepath }}" alt="" class="img-responsive"></a>
                    <div class="post-news-body">
                      <h6><a class="mouserPointer" (click)="ngxSmartModalService.getModal('myModal').open()" >{{ modules.heading }} </a></h6>
                      <div>
                        <p>{{ modules.body }}</p>
                      </div>
                      <!-- <div class="post-news-meta offset-top-20"><span class="text-black"><span class="fa fa-clock-o"></span>2 days ago</span></div> -->
                    </div>
                  </article>
                  <!-- Print popup modal alongside -->
               <ngx-smart-modal #myModal identifier="myModal">
      <h1>{{ modules.heading }}!</h1>
      <img class="img-responsive" src="./assets/images/faa.jpg" alt="">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ullamcorper neque ac ullamcorper scelerisque. Proin molestie erat sapien, ac dapibus tortor placerat a. Vivamus quis tempor mauris. Ut porta ultricies nisi in eleifend. In magna nisi, tempor pharetra consequat in, sagittis id augue. Aliquam eu lectus id mi vulputate convallis. Nam facilisis nisl quis urna gravida tempus.</p>

      <button (click)="myModal.close()" class="btn btn-danger">Close</button>
    </ngx-smart-modal>


       </div>

    </div>


  </div>

I tried several way of doing this like using the modal identifier to loop index but still didn't work. 我尝试了几种方法来执行此操作,例如使用模式标识符循环索引,但仍然无法正常工作。 like. 喜欢。

<div class="container">

    <div class="row">

     <div class="col-md-4 col-sm-6" *ngFor = "let modules of get_modules; let i = index">
          <article class="post-news"><a class="mouserPointer" (click)="ngxSmartModalService.getModal('{{i}}').open()"><img src="./assets/images/{{ modules.imagepath }}" alt="" class="img-responsive"></a>
                    <div class="post-news-body">
                      <h6><a class="mouserPointer" (click)="ngxSmartModalService.getModal('{{i}}').open()" >{{ modules.heading }} </a></h6>
                      <div>
                        <p>{{ modules.body }}</p>
                      </div>
                      <!-- <div class="post-news-meta offset-top-20"><span class="text-black"><span class="fa fa-clock-o"></span>2 days ago</span></div> -->
                    </div>
                  </article>
                  <!-- Print popup modal alongside -->
               <ngx-smart-modal #{{i}} identifier="{{i}}">
      <h1>{{ modules.heading }}!</h1>
      <img class="img-responsive" src="./assets/images/faa.jpg" alt="">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ullamcorper neque ac ullamcorper scelerisque. Proin molestie erat sapien, ac dapibus tortor placerat a. Vivamus quis tempor mauris. Ut porta ultricies nisi in eleifend. In magna nisi, tempor pharetra consequat in, sagittis id augue. Aliquam eu lectus id mi vulputate convallis. Nam facilisis nisl quis urna gravida tempus.</p>

      <button (click)="myModal.close()" class="btn btn-danger">Close</button>
    </ngx-smart-modal>


       </div>

    </div>


  </div>

Any help will be dully appreciated thanks. 任何帮助将不胜感激,谢谢。

I suggest you to declare only one <ngx-smart-modal> in your template and not loop over it. 我建议您在模板中仅声明一个<ngx-smart-modal> ,而不要在其上循环。

You can use the setModalData(...) and getModalData(...) (the name of the methods may vary depending of the version of the library you're using and if you call it from the service or the component directly). 您可以使用setModalData(...)getModalData(...) (方法的名称可能会有所不同,具体取决于所使用的库的版本以及是否直接从服务或组件中调用它)。

Then you can loop and set buttons with the data you want to display in the modal, and make the modal show the given data dynamically. 然后,您可以使用要在模式中显示的数据循环和设置按钮,并使模式动态显示给定的数据。

It's a huge performance improvement to do that, because if you put <ngx-smart-modal> in a loop, you'll force it to be declared again and again. 这样做可以极大地提高性能,因为如果将<ngx-smart-modal>放入循环中,则将迫使其一次又一次地声明。

Let only one <ngx-smart-modal> instance active that will show the data dynamically depending on which button of your loop is clicked. <ngx-smart-modal>一个<ngx-smart-modal>实例,该实例将根据单击循环的哪个按钮来动态显示数据。

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

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