【问题标题】:How to use Hidden in angular5?如何在angular5中使用隐藏?
【发布时间】:2018-05-27 00:22:44
【问题描述】:

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

出于这个原因,我使用了 hidden 属性,所以当管理员验证他只能得到“接受”时,我尝试这样做,但我遇到了一些问题。

这是我使用的代码。

如果 c.valid 为真,则出现“已接受”按钮,否则应出现“未接受”按钮。

文件干预.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();
  }
}

这可能是我想要做的吗?如果是的话,有什么想法吗?

编辑 说明:

管理员可以获得干预列表,他可以接受或不接受(有效是春季干预实体中的布尔属性),我想要的是如果有效为假,那么管理员只会看到“不接受”,然后他可以点击这个按钮以便他接受它,所以在验证方法中我应该将 c.valid 的值从 false 更改为 true ,这样只有接受的按钮才会显示给管理员,

【问题讨论】:

  • 您好,我无法理解这个问题,您想要一种获取 api 休息的方法吗?
  • 您好,感谢您的回答,让我向您解释一下,管理员可以获得干预列表,他可以接受或不接受(有效是春季干预实体中的布尔属性),我想要的是如果有效为假,那么管理员将只看到“未接受”,然后他可以单击此按钮以便他可以接受它,所以在验证方法中我应该将 c.valid 的值从假更改为真,这样只有接受的按钮才会出现在管理员面前,我不想将请求发送到 api,然后返回到 angular 并修改 pageIntervention .. 我想先修改它。
  • 那么你的代码就OK了。[hidden]="!c.valid"这是正确的使用方法,因为你在内部会改变这个对象的属性,它会立即反映变化
  • 问题是我从属性 PageIntervention 获得了valid(它是布尔值)的值,我需要调用这个方法来将'valid'属性从false修改为true。跨度>

标签: angular spring-boot angular5 angular2-services


【解决方案1】:
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>

【讨论】:

    【解决方案2】:

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

    ngIF

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

    【讨论】:

    • 问题是我从属性 PageIntervention 获得了valid(它是布尔值)的值,我需要调用这个方法来将'valid'属性从false修改为true。跨度>
    • 好吧,我现在试试这个。
    • (click)="validate(c)" 你发送的是对象 c 还是 c.id ?
    • 我正在向您发送对象引用,您可以直接更改对象属性,不要按 ID 搜索
    • 你收到了吗?
    猜你喜欢
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2015-05-14
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多