I want to develop a directive that should has follow functionality:
我想开发一个指令,它应该具有以下功能:
- Captures the last element of the element on which the directive is added
- Adds to this last element (in this case a button) the click-EventListener
- On click, a boolean should get toggled
- Based on this boolean, a new css class should get added to the base element.
In my current try I use @ContentChild to get the button. Than I added the interface AfterViewInit to the directive. In ngAfterViewInit() I register the click-event on the button with the function toggleMenu. In this function I just toggle the boolean to true or false. To add the css class to the base element I use @HostBinding() with the boolean, that is toggled.
在我当前的尝试中,我使用@ContentChild来获取按钮。然后我将接口AfterViewInit添加到指令中。在ngAfterViewInit()中,我使用函数toggleMenu注册了按钮上的点击事件。在此函数中,我只是将布尔值切换为True或False。要将css类添加到基本元素,我使用@HostBinding()和布尔值,这是切换的。
The problem that I have is, that this doesn't work. The click-event fires and the boolean value changes to, but no css class is added. I don't know why.
我的问题是,这是行不通的。将触发Click-Event,布尔值更改为,但不添加任何css类。我也不知道原因。
This is the code:
代码是这样的:
export class ToggleSidemenuDirective implements AfterViewInit {
@ContentChild('toggleBtn') element: ElementRef;
private btn: HTMLBodyElement;
ngAfterViewInit(): void {
this.btn = this.element.nativeElement;
this.btn.addEventListener('click', this.toggleMenu);
}
@HostBinding("class.sidemenu-toggled") toggled = true;
private toggleMenu(){
this.toggled = !this.toggled;
console.log(this.toggled)
}
}
Has anyone an idea, why this doesn't work?
有没有人有主意,为什么这不管用?
更多回答
Do you want to use a directive because you want this code to be reusable? If not I suggest adding this logic to the component.
您希望使用指令,因为您希望此代码可重用吗?如果没有,我建议将此逻辑添加到组件中。
In general try not to use addEventListener
with angular. Rather use (click)
in a template to do this. To me it's also not clear why you would need a directive here, move this logic to the component. And re-use the component where you need this functionality. Store the toggle status in service so it can be re-used in multiple components.
一般来说,尽量不要使用带有角度的addEventListener。而是在模板中使用(单击)来执行此操作。对我来说,也不清楚为什么这里需要一个指令,将这个逻辑移到组件中。并在需要此功能的地方重新使用该组件。将切换状态存储在服务中,以便可以在多个组件中重复使用。
I've just learned what directives are and what kind of directives exist. Now I want to use them to implement functionality like this. I decided to use a directive in this case, because I only must use this directive to add this functionality that is usually added via a component and EventBinding. To do it in a directive is much less work and you have all the code only in one place. When are you suggest to use structural directives?
我刚刚了解了什么是指令,以及存在什么样的指令。现在我想用它们来实现这样的功能。在本例中,我决定使用指令,因为我只需要使用该指令来添加通常通过组件和EventBinding添加的功能。在指令中执行此操作所需的工作量要少得多,而且您只需将所有代码放在一个位置。你建议什么时候使用结构性指令?
优秀答案推荐
ContentChild only gets the "first", you should use ContentChildren and get the last
ContentChild只获取“First”,您应该使用ContentChild并获取最后一个
//it's a QueryList
@ContentChildren('toggleBtn') elements: QueryList<ElementRef>;
private btn: HTMLBodyElement;
ngAfterViewInit(): void {
//You get the last element of the queryList using last
this.btn = this.elements.last.nativeElement;
this.btn.addEventListener('click', this.toggleMenu);
}
更多回答
我是一名优秀的程序员,十分优秀!