简体   繁体   English

角度-点击事件未在按钮上触发

[英]Angular - Click event not triggered on button

After I updated angular and angular-cli , I keep facing strange problems. 更新angularangular-cli ,我一直面临着奇怪的问题。 I have two <div> s and I am trying to just hide one and show the other when a button clicked. 我有两个<div> ,当单击按钮时,我试图隐藏一个并显示另一个。

Here is my functions: 这是我的功能:

  ngOnInit() {
    this.goalView = this.el.nativeElement.getElementsByClassName('goal-view')[0];
    this.goalEdit = this.el.nativeElement.getElementsByClassName('goal-edit')[0];
  }

  triggerEdit(): void {
    this.goalView.style.display = 'none';
    this.goalEdit.style.display = 'block';
  }

  triggerView(save: boolean): void {
    this.goalEdit.style.display = 'none';
    this.goalView.style.display = 'block';
  }

And my template: 和我的模板:

<div class="goal-view">
  ...
  <button class="ui right floated green button margin-vertical-10" (click)="triggerEdit()"><i class="ui edit icon"></i>Edit</button>
</div>
<div class="goal-edit">
  ...
  <button class="ui right floated green button margin-vertical-10"><i class="ui save icon" (click)="triggerView(true)"></i>Save</button>
</div>

When I click edit button, it works. 当我单击edit按钮时,它起作用。 When I click the save button, it works, sometimes. 当我单击“ save按钮时,有时会起作用。 But sometimes it does not, I should click the button 30-40 times to make it work. 但是有时却不行,我应该单击按钮30-40次以使其起作用。 I tried to just console.log("something") in the triggerView function, but it does the same. 我试图在triggerView函数中只使用console.log("something") ,但它的作用相同。 Sometimes works and sometimes does not. 有时有效,有时无效。

I also checked the event.target of the click events: 我还检查了click事件的event.target

document.addEventListener('click', function(evt) {
    console.log(evt.target);
}, false);

It seems normal. 看来很正常。 It prints the correct <button> when I clicked. 单击时,它将打印正确的<button>

Any idea on what should be the problem? 有什么问题的想法吗?

Generally, you should not have to deal with direct element access inside your Component. 通常,您不必在组件内部处理直接元素访问。

Rather you should switch over to using properties representing the state of elements. 相反,您应该切换到使用表示元素状态的属性。

Try this 尝试这个

<div class="goal-view" [hidden]="hideGoalView">
...
</div>

<div class="goal-edit" [hidden]="hideGoalEdit">
...
</div>


ngOnInit() {
   this.hideGoalView = false;
   this.hideGoalEdit = true;
}

triggerEdit(): void {
   this.hideGoalView = true;
   this.hideGoalEdit = false;
}

triggerView(save: boolean): void {
   this.hideGoalView = false;
   this.hideGoalEdit = true;
}

您在image标签而不是button标签上具有第二个单击侦听器。

You add events to two different elements. 您将事件添加到两个不同的元素。


Method triggerEdit is event on button tag, so you can click on whole button to called it. 方法triggerEdit按钮标签上的事件,因此您可以单击整个按钮来调用它。

Method triggerView is event on i element, so you must click small area on button to call it. 方法triggerViewi元素上的事件,因此必须单击按钮上的小区域才能调用它。


whether I suggest you take seriously @devnull69 advice. 我是否建议您认真对待@ devnull69建议。 This will save you problems in the future. 这将为您节省将来的问题。

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

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