简体   繁体   English

Angular上Bootstrap模态的奇怪行为

[英]Strange Behavior on Bootstrap Modal on Angular

I have an Angular 6 application that I use some jquery functions because I'm more comfortable with it, this is my problem : 我有一个Angular 6应用程序,我使用一些jquery函数,因为我对它更满意,这是我的问题:

I have two routes that are load by lazyload : 我有两条通过lazyload加载的路由:

painel.component.ts and osmontagem.component.ts painel.component.ts和osmontagem.component.ts

I have a modal in osmontagem.component.ts that retrieve an array that I populate a new array that is holding by a select on a bootstrap modal that is localized in my osmontagem.component.html, this is my code : 我在osmontagem.component.ts中有一个模态,用于检索一个数组,该数组填充了一个新数组,该数组由osmontagem.component.html中本地化的引导模态中的选择所保存,这是我的代码:

osmontagem.component.html ( modal part ) osmontagem.component.html(模式部分)

<div id="listAssemblers" class="modal fade listAssemblersClass" tabindex="-1" role="dialog" aria-labelledby="Lista de Montadores" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Pim: <strong>{{ this.osData.wt_os_pim }}</strong></h5> <h5>&emsp;|&emsp;</h5><h5 class="modal-title">Lista de Montadores</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
        <div class="row">

          <div class="col-md-12">
            <button (click)="sendAssemblersToServer(this.osData.wt_os_pim)" type="button" class="btn btn-secondary btn-default-modal btn-import pointer">Adicionar Selecionados</button>
          </div>

          <div class="col-md-12">
            <div class="table-responsive m-t-20">
              <table class="table m-b-0 f-14 b-solid requid-table table-import">
                <thead>
                  <tr class="text-uppercase">                    
                    <th>Montador</th>                  
                  </tr>
                </thead>
                <tbody class="text-muted">
                  <tr>                   
                    <td>                     
                        <select id="select-assembler-modal" class="js-example-basic-multiple col-sm-12 select-modal" multiple="multiple">
                          <ng-container *ngFor="let field of newListAssPim">                    
                            <option value="{{ field.wt_assembler_id }}" [selected]="field.checked">{{ field.wt_assembler_name }}</option>
                          </ng-container>
                        </select>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
        </div>
      </div>
    </div>
  </div>
</div>

osmontagem.component.ts osmontagem.component.ts

public chooseAssemblers(row: any, data: any, index: number) {

    this.loading = true;
    this.osData = data;
    this.rowIndex = index;
    this.assemblerService.getAssemblersDataByPim(this.osData.wt_os_pim).subscribe(
      response => {
        const len2 = this.assemblers.length;
        if (response.success !== 0) {

          const listAssemblersByPim = response.data;
          const len = listAssemblersByPim.length;
          if (len > 0) {
            for (let i = 0; i < len2; i++) {
              let ismatch = false;
              for (let j = 0; j < len; j++) {

                if (this.assemblers[i].wt_assembler_id === listAssemblersByPim[j].wt_assembler_id) {
                  const paramAss = new ParamAssemblerPim(this.assemblers[i].wt_assembler_name,
                                                       this.assemblers[i].wt_assembler_id,
                                                       this.osData.wt_os_pim, true);
                  this.newListAssPim.push(paramAss);
                  ismatch = true;
                  break;
                }
              }
              if (!ismatch) {
                const paramAss = new ParamAssemblerPim(this.assemblers[i].wt_assembler_name,
                                                     this.assemblers[i].wt_assembler_id,
                                                     this.osData.wt_os_pim, false);
                this.newListAssPim.push(paramAss);
              }
            }
          }
        } else {

          for (let i = 0; i < len2; i++) {
            const paramAss = new ParamAssemblerPim(this.assemblers[i].wt_assembler_name,
                                                 this.assemblers[i].wt_assembler_id,
                                                this.osData.wt_os_pim, false);
            this.newListAssPim.push(paramAss);
          }

        }

        this.loading = false;
        $('.listAssemblersClass').appendTo('body').modal('show');
         $('.js-example-basic-multiple').each(function (i, obj) {
          if (!$(obj).data('select2')) {
              $('.js-example-basic-multiple').select2();
          }
        });

      });

  }

this function chooseAssemblers() start when I click button from a row in a list, It is working fine when my application starts and I go direct osmontagem.component.html, this picture shows the approach that I want : 当我从列表中的一行中单击按钮时,此函数chooseAssemblers()启动,当我的应用程序启动并且直接转到osmontagem.component.html时,它工作正常,此图显示了我想要的方法:

PICTURE 1 ( Approach that I want ) 图片1(我想要的方法)

But when I go to painel.component.html route and go back to this route again and simply press the button again, the application open 2 modals and my list is gone, why this is happening? 但是,当我转到painel.component.html路由并再次返回此路由并再次按下按钮时,应用程序打开了2个模态,我的列表不见了,为什么会这样? See the picture below 见下图

PICTURE 2 图片2

this line $('.listAssemblersClass').appendTo('body').modal('show'); 这行$('。listAssemblersClass')。appendTo('body')。modal('show'); have a class reference, when i put # id reference they don't show 2 modals, but my list is gone, Why? 有一个类引用,当我输入#id引用时,它们不显示2个模态,但是我的列表不见了,为什么?

This is happening only if i go out from the route, go to another and comeback. 仅当我从路线中走出来,去另一个并复出时,这种情况才会发生。 I debug if is something wrong with my array but the object always stay populated. 我调试数组是否有问题,但是对象始终保持填充状态。

I managed to get this working with a strange change on DOM, the problem is that when I first load the component it loads and do the work correctly but when I change the pages is like the ID element from modal is not changing and every time I call this function 我设法在DOM上进行了一次奇怪的更改,使问题得以解决,问题是,当我第一次加载组件时,它会加载并正确地工作,但是当我更改页面时,就像模态中的ID元素一样,每次都不会更改调用此功能

$('.listAssemblersClass').appendTo('body').modal('show'); $( 'listAssemblersClass ')appendTo('主体')模式('显示')。;

To show the modal, now I change to this, everytime the user go to the page my component generate a random ID element for the modal like this: 为了显示模式,现在我更改为此模式,每当用户转到页面时,我的组件都会为模式生成一个随机ID元素,如下所示:

$('#listPrdModal' + this.idRandomNumber).appendTo('body').modal('show'); $('#listPrdModal'+ this.idRandomNumber).appendTo('body')。modal('show');

And now is working fine, I dunno how can I reset the HTML page from the component? 现在工作正常,我不知道如何从组件中重置HTML页面? To not use this approach? 要不使用这种方法? If no one gives me an answer for a better approach this is the answer. 如果没有人给我一个更好的方法的答案,那就是答案。

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

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