简体   繁体   English

单击在角度不起作用

[英]Click does not work in angular

Below is the code.下面是代码。 The click event does not work with a nav-burger and a hidden menu.单击事件不适用于导航汉堡和隐藏菜单。

menu.component:菜单组件:

section class="hero outer is-primary is-fullheight animated slideInLeft" [ngClass]="{'is-hidden' : !menu.opened}">

menu.service.ts: menu.service.ts:

@Injectable()
export class MenuService {
  opened: boolean;

  constructor() { }
  open (){
    this.opened = true;
  }

}

header.component.ts: header.component.ts:

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

  constructor(public menu:MenuService) { 

header html:标题html:

 <div class="navbar-burger burger" (click)="menu.open()">
      <span></span>
      <span></span>
      <span></span>
    </div>

Instead of trying to bind to a property of MenuService, have MenuService emit an event:与其尝试绑定到 MenuService 的属性,不如让 MenuService 发出一个事件:

menu.service.ts: menu.service.ts:

@Injectable()
export class MenuService {
  opened = new EventEmitter<boolean>();

  constructor() { }
  open (){
    this.opened.emit(true);
  }
}

then listen to that event in your other component's and bind it to a local property.然后在其他组件中侦听该事件并将其绑定到本地属性。

export class HeaderComponent implements OnInit {
  opened: boolean;

  constructor(public menu: MenuService) { }

  ngOnInit() {
    this.menu.opened.subscribe((event) => { 
      this.opened = event;
    }
  }
}

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

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