gpt4 book ai didi

Angular4 基本指令不起作用

转载 作者:行者123 更新时间:2023-12-04 12:46:45 26 4
gpt4 key购买 nike

我正在尝试使用自定义指令更改 div 的背景颜色,但它不起作用,下面是我使用 angular cli 生成的组件的代码。

import {
Component,
Directive,
Renderer,
ElementRef,
NgModule,
OnInit } from '@angular/core';

@Directive({
selector:"[ccCardHover]"
})
class CardHOverDirective {
constructor(private el: ElementRef,
private renderer: Renderer) {
renderer.setElementStyle(el.nativeElement, 'backgroundColor', 'blue');
}
}
@Component({
selector: 'app-custom-directive',
template: `
<div class="panel panel-default" ccCardHover>
<p class="panel-body">Body text</p>
</div>`
})
export class CustomDirectiveComponent implements OnInit {
ngOnInit() {}
}

为什么不将“panel panel-default”的背景设为蓝色?

最佳答案

在仔细检查我的代码后,我注意到两件事

  1. 导出自定义指令:

    导出类 CardHoverDirective { ..

  2. 在app.module.ts中导入并声明

关于Angular4 基本指令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44860271/

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