简体   繁体   中英

How to use Hidden in angular5?

I'm working with angular5 as front end , spring-boot as back end , here in the code below i have a list of intervention that the admin can accept or not.

For this reason i used the hidden attribute so when the admin validates he can gets only 'accepted' , i tried to do that but i face some problems .

Here is the code i used .

if c.valid is true the button of 'Accepted' appears, else the 'Not Accepted' button should appear .

File 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>

File 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();
  }
}

IS it possible what i'm trying to do ? if it's yes any idea on how to do it ?

EDIT Explanation :

The admin can gets a list of interventions , that he can accept or not (valid is a boolean attribute in intervention Entity in spring ) , what i want is if valid is false then the admin will see only 'Not accepted' , then he can click on this button so he can accept it , so in validate method i should change the value of c.valid from false to true , this way only the button accepted will apear to the admin ,

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>

Ok, try by using ngIf because the logic is a little bit confused, and in the function validate send directly the object reference and modify.

ngIF

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

[hidden]

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

Component

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;

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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