gpt4 book ai didi

angular - 如何将值传递给 Angular Directive(指令)

转载 作者:太空狗 更新时间:2023-10-29 19:26:34 24 4
gpt4 key购买 nike

代码和说明已更新

我希望该指令根据 bool 值禁用或启用,但无论我通过 isDraggable 变量发送什么值(真/假),在这两种情况下,指令都已启用。

这段代码需要改进什么?

@Directive({
selector: '[movableObject]'
})
export class MovableDirective extends DraggableDirective {
@Input() movableObject: boolean;
}

@Component({
selector: 'app-panel',
template: `<div [movableObject]="isDraggable"></div>`,
styleUrls: ['./panel.component.scss'],
})
export class PanelComponent implements OnInit {
private isDraggable: boolean = true;
}

最佳答案

注意@Input 装饰器。它将元数据添加到类中,使指令的 movableObject 属性可用于绑定(bind)。

@Directive({
selector: '[movableObject]'
})
export class MovableDirective extends DraggableDirective {
@Input() movableObject: boolean;
}

@Component({
selector: 'app-panel',
template: `<div [movableObject]="isDraggable"></div>`,
styleUrls: ['./panel.component.scss'],
})
export class PanelComponent implements OnInit {
private isDraggable: boolean;
}

关于angular - 如何将值传递给 Angular Directive(指令),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com