我正在angular 5项目中使用ngx-smart-modal,当不在* ngFor循环中时,ngx-smart-modal可以完美工作。我想要的只是从数据库中以抽象方式获取数据并根据循环数显示。这是我的代码。
<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>
我尝试了几种方法来执行此操作,例如使用模式标识符循环索引,但仍然无法正常工作。喜欢。
<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>
任何帮助将不胜感激。
答案 0 :(得分:2)
我建议您在模板中仅声明一个<ngx-smart-modal>
,不要在其上循环。
您可以使用setModalData(...)
和getModalData(...)
(方法的名称可能会有所不同,具体取决于所使用的库的版本以及是否直接从服务或组件中调用)
然后,您可以循环和设置按钮,使其具有要在模式中显示的数据,并使模式动态显示给定的数据。
这样做是对性能的巨大改进,因为如果将<ngx-smart-modal>
放入循环中,则会迫使其一次又一次地声明。
仅激活一个<ngx-smart-modal>
实例,该实例将根据单击循环的哪个按钮来动态显示数据。