【问题标题】:Angular 2 hide and show element using *ngIf with booleanAngular 2 使用带有布尔值的 *ngIf 隐藏和显示元素
【发布时间】:2017-05-11 01:40:24
【问题描述】:

我想通过使用位于另一个组件中的按钮来显示和隐藏一个元素。

所以我有一个仪表板,里面有很多房间和一个标题。

带有 app-header 和 app-chamber 的 DashboardComponent 的 HTML:

 <app-header></app-header>
    <div class="container">
      <div class="row">
        <app-chamber [kamers]="kamers"></app-chamber>
      </div>
    </div>

我的 ChamberComponent 中有这个带有 *ngIf 的 HTML:

<div class="col-sm-6 col-md-4 col-lg-3 cardcol" *ngFor="let kamer of kamers; let i = index">
      <md-card class="chamber wit" *ngIf="kamer.patient == null">
         <p *ngIf="showId">{{kamer.id}}</p>
      </md-card>
</div>

在 HeaderComponent 我有一个需要显示和隐藏元素的按钮:

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

  @Input() aList;

  dashboardComponent:DashboardComponent;

  chamberComponent:ChamberComponent;

  constructor(dashboardComponent: DashboardComponent, chamberComponent:ChamberComponent) {
    this.dashboardComponent = dashboardComponent;
    this.chamberComponent = chamberComponent;

  }

  ngOnInit() {

  }

// THIS GETS CALLED BY A BUTTON CLICK
  toggleId(){
    this.chamberComponent.toggleId();
  }

}

然后是我的 ChamberComponent 代码:

@Component({
  selector: 'app-chamber',
  templateUrl: './chamber.component.html',
  styleUrls: ['./chamber.component.css']
})
      export class ChamberComponent implements OnInit {

      @Input() kamers;
      showId:boolean;

      constructor() {
        this.showId=false;
      }

      ngOnInit() {

      }

      toggleId = () => {
          this.showId = !this.showId;
      }

    }

所以当我点击按钮时,值会发生变化(我已经在控制台中记录了这个)但视图没有更新..

当我在 ChamberComponent 中放置一个调用 toggleId() 函数的按钮时,视图确实会收到更新,并且元素会显示或隐藏。

但我需要从标题上的按钮切换它。

【问题讨论】:

  • 副本是关于回调范围内的“this”。但是,我的代码应该根据该示例工作。 this.showId 不工作..
  • 如果完全没有显示或隐藏,并且您没有其他内容,您确定满足*ngIf="kamer.patient == null"的条件吗?
  • 这就是问题所在,这应该非常重要地添加到问题中,因为它是非常相关的信息!所以请将所有相关代码放在帖子中! :)
  • 我已经更新了问题,谢谢!

标签: angular angular-ng-if


【解决方案1】:

Plunker

@Input() kamers 和模板 *ngIf="kamer.patient == null" 之间有一点不匹配。

  • 只需将输入更改为kamer

模板 HTML

<md-card class="chamber wit" *ngIf="kamer.patient === null">
   <p *ngIf="showId">{{kamer.id}}</p>
</md-card>

<button (click)="toggleId()">Toggle</button>

组件

@Component({
  selector: 'material-app',
  templateUrl: 'app.component.html'
})
export class AppComponent {

  kamer = {
    patient: null,
    id: '1'
  };
  showId = false;

  ngOnInit() {

  }

  toggleId() {
    this.showId = !this.showId;
  }

}

更新 (1) - Plunker (1)

使用@ViewChild

要引用子组件的函数,请使用ViewChild

  • @ViewChild('child') child;放在父组件中
  • 在父模板中引用子函数如下:&lt;button (click)="child.toggleId()"&gt;Toggle&lt;/button&gt;

父组件

@Component({
  selector: 'material-app',
  template: `
    <material-child #child></material-child>
    <button (click)="child.toggleId()">Toggle</button>
  `
})
export class AppComponent {

  @ViewChild('child') child;

}

子模板

<md-card class="chamber wit" *ngIf="kamer.patient === null">
   <p *ngIf="showId">{{kamer.id}}</p>
</md-card>

子组件

@Component({
  selector: 'material-child',
  templateUrl: 'app.component.html'
})
export class ChildComponent {

  kamer = {
    patient: null,
    id: '1'
  };
  showId = false;

  ngOnInit() {

  }

  toggleId() {
    this.showId = !this.showId;
  }

}

更新 (2) - Plunker (2)

使用@Output() + EventEmitter

要扩展之前的设置以使用同级组件,您可以

  • 让兄弟姐妹使用EventEmitter 发出事件
  • 监听父组件发出的事件,并使用ViewChild调用需要的子函数

兄弟组件

@Component({
  selector: 'material-sibling',
  template: `
    <button (click)="toggle.emit()">Toggle</button>
  `
})
export class SiblingComponent {
  @Output() toggle = new EventEmitter();
}

父组件

@Component({
  selector: 'material-app',
  template: `
    <material-child #child></material-child>
    <material-sibling (toggle)="child.toggleId()"></material-sibling>
  `
})
export class AppComponent {

  @ViewChild('child') child;

}

子模板

<md-card class="chamber wit" *ngIf="kamer.patient === null">
   <p *ngIf="showId">{{kamer.id}}</p>
</md-card>

子组件

@Component({
  selector: 'material-child',
  templateUrl: 'app.component.html'
})
export class ChildComponent {

  kamer = {
    patient: null,
    id: '1'
  };
  showId = false;

  ngOnInit() {

  }

  toggleId() {
    this.showId = !this.showId;
  }

}

【讨论】:

  • 感谢您的宝贵时间,我已经用更多信息更新了这个问题。所以基本上如果我听从你的建议,我有 DashboardComponent 的 2 个孩子(应用程序室和应用程序头)。现在您的示例显示了如何从父级调用子级的函数。我需要从一个孩子向另一个孩子调用一个函数。
  • 也许最好/最简单的解决方案是将标题放在仪表板 html 中并按照您说的那样做?
  • 好的,我想我已经为兄弟设置准备好了。如果您需要其他任何东西,请告诉我,谢谢!
  • 你先生,是个英雄!
  • 谢谢,很高兴有帮助!
猜你喜欢
  • 2015-03-13
  • 1970-01-01
  • 2016-05-11
  • 2020-02-05
  • 2018-08-04
  • 2021-03-07
  • 2019-06-12
  • 2019-10-01
  • 2021-12-20
相关资源
最近更新 更多