如何获取ngb手风琴的打开/关闭状态



我想根据面板是展开还是折叠,在手风琴的ngb-panels标题中呈现两个不同的图标/div。面板内部有一些表单内容。因此,当面板关闭时,它会显示一个图标,而当它打开时,它必须显示另一个图标。

HTML和CSS模板示例

input[type="text"],
select.form-control{
  background: transparent;
  border: none;
  border-bottom: 1px solid #000000;
  -webkit-box-shadow: none;
  box-shadow: none;
  border-radius: 0;
}
input[type="number"],
select.form-control{
  background: transparent;
  border: none;
  border-bottom: 1px solid #000000;
  -webkit-box-shadow: none;
  box-shadow: none;
  border-radius: 0;
}
input[type="text"]:focus,
select.form-control:focus {
  -webkit-box-shadow: none;
  box-shadow: none;
  border-color:rgb(31, 14, 187);
}
input[type="number"]:focus,
select.form-control:focus {
  -webkit-box-shadow: none;
  box-shadow: none;
  border-color:darkred;
}
::ng-deep .collapse {
    transition: max-height .55s, opacity .35s ease-in-out;
    max-height: 0;
    opacity: 0;
    display: block !important;
  
    &.show {
      max-height: 100rem;
      opacity: 1;
    }
  }
  
  ::ng-deep .accordion {
    .card {
      margin-bottom: 0 !important;
      border-bottom: 1px solid rgba(0, 0, 0, .04) !important;
  
      .card-header {
        // padding-top: 0;
        color: #000000;
        padding: 0;
  
        button {
          padding: 1.2rem;
          width: 100%;
  
          span {
              color: #5f5f5f;
            float: left;
            font-size: 1.2rem;
          }
        }
      }
  
      .card-body {
        padding: 1rem;
      }
    }
  }
 <head><!-- CSS only --> 
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
</head>
 <body>
 <ngb-accordion #acc="ngbAccordion" [destroyOnHide]='false' [closeOthers]="true" activeIds="ngb-panel-0">
        <ngb-panel id="panel1">
            <ng-template ngbPanelTitle>
              <span *ngIf=""> <!-- condition when it is closed -->
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  width="20"
                  height="20"
                  fill="currentColor"
                  class="bi bi-x-circle"
                  viewBox="0 0 16 16"
                >
                  <path
                    d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"
                  />
                  <path
                    d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"
                    style="color: red;border-color: red;"
                  /></svg
                >&nbsp;</span>
              <span *ngIf="" ><!-- condition when it is opened -->
                <svg
                  
                  xmlns="http://www.w3.org/2000/svg"
                  width="20"
                  height="20"
                  fill="currentColor"
                  class="bi bi-check-circle"
                  viewBox="0 0 16 16"
                >
                  <path
                    d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"
                  />
                  <path
                    d="M10.97 4.97a.235.235 0 0 0-.02.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-1.071-1.05z"
                    style="color: green;"
                  /></svg
                >&nbsp;</span>
                
              <span>Primary Details </span>
              <span style="margin-left: 85%;"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-plus-circle" viewBox="0 0 16 16">
                <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
                <path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z" style="color: black;background-color: black;"/>
              </svg></span>
            </ng-template>
            <ng-template ngbPanelContent>
             some content
            </ng-template>
          </ngb-panel>
          <ngb-panel id="panel2">
            <ng-template ngbPanelTitle>
                <span *ngIf=" "> <!--condition when it is closed -->
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="20"
                    height="20"
                    fill="currentColor"
                    class="bi bi-x-circle"
                    viewBox="0 0 16 16"
                  >
                    <path
                      d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"
                    />
                    <path
                      d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"
                      style="color: red;border-color: red;"
                    /></svg
                  >&nbsp;</span>
                <span *ngIf=""> <!-- condition when it is opened -->
                  <svg
                    
                    xmlns="http://www.w3.org/2000/svg"
                    width="20"
                    height="20"
                    fill="currentColor"
                    class="bi bi-check-circle"
                    viewBox="0 0 16 16"
                  >
                    <path
                      d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"
                    />
                    <path
                      d="M10.97 4.97a.235.235 0 0 0-.02.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-1.071-1.05z"
                      style="color: green;"
                    /></svg
                  >&nbsp;</span>
                <span>Secondary Details </span>
                <span style="margin-left: 83.3%;"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-plus-circle" viewBox="0 0 16 16">
                    <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
                    <path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z" style="color: black;background-color: black;"/>
                  </svg></span>
              </ng-template>
            <ng-template ngbPanelContent>
               <form #form2="ngForm" style="padding-left:20px;">
                
                   some more content    
                     </form> 
            </ng-template>
         </ngb-panel>
          </ngb-accordion>
          </body>

因此,在*ngIf=""检查面板是关闭还是打开的状态,并呈现相应的跨度。请帮我找到检查面板状态的最佳方法。我观察到有几个问题,但它们不起作用。

由于代码的和平性,您在acc变量中引用了手风琴:#acc="ngbAccordion"。要检查面板是否展开,可以使用isExpanded方法。

<ngb-accordion #acc="ngbAccordion" [destroyOnHide]='false' [closeOthers]="true" activeIds="ngb-panel-0">
   <ngb-panel id="panel1">
      <div>isActive: {{acc.isExpanded('panel1')}}</div>      
   </ngb-panel>
   <ngb-panel id="panel2">
      <div>isActive: {{acc.isExpanded('panel2')}}</div>      
   </ngb-panel>
</ngb-accordion>

最新更新