代码之家  ›  专栏  ›  技术社区  ›  dEs12ZER

如何使用隐藏在角度5?

  •  0
  • dEs12ZER  · 技术社区  · 7 年前

    我正在使用Angular5作为前端,SpringBoot作为后端,在下面的代码中,我有一个管理员可以接受或不接受的干预列表。

    出于这个原因,我使用了hidden属性,所以当管理员确认他只能被“接受”时,我试图这样做,但我面临一些问题。

    这是我使用的代码。

    如果c.valid为true,则显示“已接受”按钮,否则应显示“未接受”按钮。

    文件intervention.component.html

     <!--/.col-->
    <div class="col-lg-12">
      <div class="card">
        <div class="card-header">
          <i class="fa fa-align-justify"></i> Les interventions
        </div>
        <div class="card-body">
          <table class="table table-striped">
            <thead>
            <tr>
              <th>Numéro</th>
              <th>objet</th>
              <th>date</th>
              <th>Etat</th>
            </tr>
            </thead>
            <tbody>
    
            <tr *ngFor="let c of pageIntervention?.content">
              <td>{{c.id}}</td>
              <td>{{c.objet}}</td>
              <td>{{c.date}}</td>
              <td>
                <button type="button"  [hidden]="c.valid" (click)="validate(c.id)">Not accepted</button>
                <button type="button" [hidden]="!c.valid">Accepted</button>
    
              </td>
            </tr>
    
            </tbody>
          </table>
    
          <ul class="pagination">
            <li class="page-item" [ngClass]="{'active':i==currentPage}" *ngFor="let p of pages ; let i=index">
              <a class="page-link" (click)="gotoPage(i)">{{i}}</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    

    文件intervention.component.ts

      export class InterventionComponent implements OnInit {
    
      pageIntervention:any;
      pages:Array<number>;
      currentPage:number=0;
      page:number=0;
      size:number=5;
      id:number;
    
      constructor(private intervService: InterventionService,
                  private authService: AuthenticationService ) { }
    
    
      ngOnInit() {
        this.id  = this.authService.getAuthenticatedUserId();
        this.Allinterventions();
      }
    
      Allinterventions(){
        this.intervService.getAllinterventions(this.page,this.size,this.id)
          .subscribe((data:any)=>{
            this.pageIntervention = data;
          },err=>{
            console.log('there is an error  ');
          })
      }
    
      validate(id:number){
          // here i want to modify c.valid so it changes from false to true .
    
         this.intervService.ValidateIntervention(id); // i call the service 
         method to send the update Request to the API rest (spring). 
      }
    
    
      gotoPage(i:number){
        this.currentPage = i;
        this.Allinterventions();
      }
    }
    

    有没有可能我要做的?如果是的,你知道怎么做吗?

    编辑 说明:

    管理员可以获取干预列表,他可以接受或不接受(valid是Spring中干预实体的布尔属性),我想要的是,如果valid为false,那么管理员将只看到“不接受”,然后他可以单击此按钮以接受它,因此在validate方法中,我将显示uld将c.valid的值从false更改为true,这样只有接受的按钮才会出现在admin中,

    2 回复  |  直到 7 年前
        1
  •  0
  •   Abel Valdez    7 年前

    好的,尝试使用 ngIf 因为逻辑有点混乱,而且在函数中 validate 直接发送对象引用和修改。

     <button type="button"  *ngIf="c.valid" (click)="validate(c)">Not accepted</button>
    <button type="button"   *ngIf="!c.valid">Accepted</button>
    

    [隐藏]

     <button type="button"  [hidden]="!c.valid" (click)="validate(c)">Not accepted</button>
    <button type="button" [hidden]="c.valid">Accepted</button>
    

    组件

    validate(ref: any){
          // here i want to modify c.valid so it changes from false to true .
          // i want to change it in pageIntervention first and then call the api
             rest to change it in database  . 
    
         //this.intervService.ValidateIntervention(id); // i call the service 
         //method to send the update Request to the API rest (spring).
    
         ref.valid = !ref.valid;
    
      }
    
        2
  •  1
  •   Ibrahim Shah    7 年前
    Simply use *ngIf as below
    <button type="button"  *ngIf="c.valid; else accepted" (click)="validate(c.id)">Not accepted</button>
    <ng-template #accepted>
    <button type="button" >Accepted</button>
    <ng-template>