简体   繁体   English

角度2输出和eventemitter不发射

[英]angular 2 output and eventemitter doesn't emit

i'm learning angular 2, and i follow this tutorial: https://egghead.io/lessons/angular-2-angular-2-building-a-toggle-button-component but the whole part of the output and eventemitter doesn't work. 我正在学习angular 2,并且按照本教程进行操作: https ://egghead.io/lessons/angular-2-angular-2-building-a-toggle-button-component但输出和eventemitter的整个部分都没有不行 i do not get any errors and i can't figure out why it doesn't work. 我没有收到任何错误,我也不知道为什么它不起作用。

this is my code for the togglelink component: 这是我的togglelink组件代码:

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';



@Component({
  moduleId: module.id,
  selector: 'app-togglelink',
  templateUrl: 'togglelink.component.html',
  styleUrls: ['togglelink.component.css']
})
export class TogglelinkComponent implements OnInit {

  @Input() state:boolean = true;
  @Output() onChange = new EventEmitter();

  onClick(){
    this.state = !this.state;
    this.onChange.emit(this.state);
  }

  constructor() {}

  ngOnInit() {
  }

}

and this is the code for the firstpage component that uses the togglelink component: import { Component, OnInit } from '@angular/core'; 这是使用切换链接组件的首页组件的代码:从'@ angular / core'导入{Component,OnInit}; import {TogglelinkComponent} from '../togglelink/togglelink.component'; 从“ ../togglelink/togglelink.component”导入{TogglelinkComponent};

@Component({
  moduleId: module.id,
  selector: 'app-firstpage',
  templateUrl: 'firstpage.component.html',
  styleUrls: ['firstpage.component.css'],
  directives: [TogglelinkComponent]
})
export class FirstpageComponent implements OnInit {

  thetogglestate:boolean = false;

  firsttitle = "the first title";
  constructor() {}

  ngOnInit() {
  }

}

and this is the code for the template of the firstpage component: {{thetogglestate ? 这是首页组件模板的代码:{{thetogglestate? 'On' : 'Off'}} '开关'}}

</p>
<h2 *ngIf="thetogglestate">
  {{firsttitle}}
</h2>
<p>
  firstpage works!
</p>

when i change manually thetogglestate it does work, so i understand that the issue is with the output and the eventemitter part. 当我手动更改togglestate时,它确实起作用,因此我知道问题出在输出和事件发射器部分。

any idea why? 知道为什么吗?

best regards 最好的祝福

In the firstpage.component.html template, you need to register some processing for this event to toggle the value of the thetogglestate variable. firstpage.component.html模板中,您需要为此事件注册一些处理,以切换thetogglestate变量的值。

Something like that: 像这样:

<app-togglelink (onChange)="thetogglestate = !thetogglestate"></app-togglelink>

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

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