简体   繁体   English

如何在angular5中使用“隐藏”?

[英]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. 我正在使用angular5作为前端,使用spring-boot作为后端,在下面的代码中,我列出了管理员可以接受或不接受的干预措施。

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 . 由于这个原因,我使用了hidden属性,因此当管理员验证他只能得到“接受”时,我试图这样做,但是我遇到了一些问题。

Here is the code i used . 这是我使用的代码。

if c.valid is true the button of 'Accepted' appears, else the 'Not Accepted' button should appear . 如果c.valid为true,则显示“已接受”按钮,否则应显示“未接受”按钮。

File intervention.component.html 文件干预.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 文件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 , 管理员可以获取他可以接受或不接受的干预列表(有效是spring干预实体中的布尔属性),我想要的是,如果有效为假,则管理员将仅看到“未接受”,那么他可以单击此按钮,以便他可以接受它,因此在验证方法中,我应该将c.valid的值从false更改为true,这样,只有接受的按钮才会出现在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因为逻辑有些混乱,并且在函数validate直接发送对象引用并进行修改。

ngIF 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;

  }

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

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