简体   繁体   English

Angular 使用 ngx-smart-modal 将参数从 html 传递到组件

[英]Angular pass parameters from html to component using ngx-smart-modal

I am using ngx-smart-modal in my project to pass value from html to component when is open.我在我的项目中使用 ngx-smart-modal 在打开时将值从 html 传递给组件。 According to official doc of ngx-smart-modal l can handle events directly from the view.根据ngx-smart-modal 的官方文档,我可以直接从视图中处理事件。 but l have undefined parameters.但我有未定义的参数。

HTML: HTML:

 <div class="card" *ngFor="let item of storeData">                         <!--  pass from here -->
 <div class="card-body text-right"(click)="ngxSmartModalService.getModal('addjob').open(item)">
  <a> 
 {{item?.title?.catname}}
 </a>
  </div>
 </div>

<!--  modal view -->
  <ngx-smart-modal #addjob identifier="addjob">
   <h1>hello</h1>
 </ngx-smart-modal>

Ts Ts

  constructor(public ngxSmartModalService: NgxSmartModalService) { }

    ngAfterViewInit() {
      this.ngxSmartModalService.getModal('addjob').onOpen.subscribe((modal) => {
        console.log('opened!', modal.getData());
      });
    }

output console: undefined any idea please? output console: undefined有什么想法吗?

Finally i got my own solution.最后我得到了自己的解决方案。

TS: TS:

  openaddjob(a){
    this.ngxSmartModalService.create('addjob', 'content').open();
    this.ngxSmartModalService.setModalData(a, 'addjob');

    console.log(a)
  }

HTML HTML

 <div class="card" *ngFor="let item of storeData">
 <div class="card-body text-right"(click)="openModal(item)">
  <a> 
 {{item?.title?.catname}}
 </a>
  </div>
 </div>

<!--  modal view -->
  <ngx-smart-modal #addjob identifier="addjob">
   <div *ngIf="addjob.hasData()">
    <pre>{{ addjob.getData() | json }}</pre>
  </div>
    <!-- output
    {
        "$key": "12122211",
        "title": {
          "catname": "Food"
        }
      } -->
 </ngx-smart-modal>

When you try to open the modal programmatically just set data forcefully before opening modal.当您尝试以编程方式打开模态时,只需在打开模态之前强制设置数据即可。

this.ngxSmartModalService.setModalData({val: 1}, 'yourModalIdenitifier', true);
this.ngxSmartModalService.open("yourModalIdenitifier");

On another component (where you have a modal)在另一个组件上(你有一个模态)

ngAfterViewInit() {
this.ngxSmartModalService.getModal('yourModalIdenitifier').onOpen.subscribe((modal) => {
  console.log('opened!', modal.getData()); // will print opened! {val: 1}
}); 
}

Happy Coding!!快乐编码!

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

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