【问题标题】:Detect if a boolean changes Angular 8检测布尔值是否更改Angular 8
【发布时间】:2021-02-09 15:23:48
【问题描述】:

我的 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);
}

【问题讨论】:

标签: html angular typescript observable


【解决方案1】:

我建议你在 Angular 中使用事件绑定

在 Angular 8 中,事件绑定用于处理从 DOM 引发的事件,例如按钮单击、鼠标移动等。当 DOM 事件发生(例如单击、更改)时,它会调用组件中的指定方法。

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

【讨论】:

  • 我想在点击时绑定,但我试过
    并且该方法不会被调用。
【解决方案2】:

在html文件中

&lt;button (click)="isCollapsedInService($event)"&gt;Btn&lt;/button&gt; &lt;!--Event Binding--&gt;

在 .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);    
  }    
}   

【讨论】:

    猜你喜欢
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 2023-01-20
    • 2014-01-06
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多