检测布尔值是否改变 Angular 8

Detect if a boolean changes Angular 8

我的 html:

中有这个 ngbCollapse
<div style="margin: 1.5em 1.5em;" [ngbCollapse]="isCollapsed">
//things
</div>

在我的组件中,每次 isCollapsed 更改时我都会调用一个方法:

export class Component implements OnInit {

public isCollapsed = true;

constructor(private Serv: _service) {}

  ngOnInit() {}

//Everytime isCollapsed changes I would call this method
isCollapsedInService () {
 this._service.set(isCollapsed);
}

我建议您在 Angular

中使用事件绑定

在Angular8中,事件绑定用于处理从DOM引发的事件,如按钮点击、鼠标移动等。当DOM事件发生时(例如点击, change), 调用组件中的指定方法

https://www.javatpoint.com/event-binding-in-angular-8#:~:text=In%20Angular%208%2C%20event%20binding,specified%20method%20in%20the%20component.

在html文件中

<button (click)="isCollapsedInService($event)">Btn</button> <!--Event Binding-->

在 .ts 文件中

import { Component } from '@angular/core';    
@Component({    
  selector: 'app-root',    
  templateUrl: './app.component.html',    
  styleUrls: ['./app.component.css']    
})    
export class AppComponent {      
 isCollapsedInService($event){    
    console.log("Button is clicked!", $event);    
  }    
}