- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用指令绑定(bind)到 ele 的 style prop
import {Directive, OnInit, ElementRef, HostListener, HostBinding} from "@angular/core";
@Directive({
"selector":"[high-light-direc]"
})
export class HighlightDirec implements OnInit{
@HostBinding('style') style:{backgroundColor:string};
constructor(private elementRef:ElementRef){}
ngOnInit(){
}
@HostListener('mouseenter') x(){
this.style.backgroundColor = 'blue';
}
@HostListener('mouseleave') y(){
this.style.backgroundColor='transparent';
}
}
<div>
<p high-light-direc>
{{toShow}}
</p>
</div>
当我尝试为 backgroundColor 属性赋值时,我收到此错误 TypeError: Cannot set property 'backgroundColor' of undefined 。
那么如何为 Binded 样式 obj 中的 backgroundColor 属性赋值。
最佳答案
Take a look at the code:
Explanation:
this.sanitizer.bypassSecurityTrustStyle
@HostBinding('style') style: SafeStyle;
然后按如下方式设置值:
@HostListener('mouseenter') x() {
this.style = this.sanitizer.bypassSecurityTrustStyle("background-
color: lime");
}
@HostListener('mouseleave') y() {
this.style = this.sanitizer.bypassSecurityTrustStyle("background-color: transparent");
}
关于Angular 4 @HostBinding 绑定(bind)到 style 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212855/
我正在寻找使用异步值处理 HostBinding 的最佳方法。 在 Angular v2.1.2 之前,我可以像这样在 @Directive 装饰器中使用 host 属性: @Directive({
我已在我的 Angular 6 应用程序中成功使用 @HostBinding 将属性应用于主机组件,就像在变量为真时应用类一样: @HostBinding('class.compact-ui') is
找不到它的文档。 从示例中我得到了 3 个案例: @HostBinding(" attr .something") @HostBinding(" class .something") @HostBin
我想知道在使用组件的 @HostBinding 和 host 属性之间是否存在巨大差异(如果存在,差异是什么?)? 我在使用动画时一直在问自己这个问题,因为我在这些情况下(看起来很接近): @Comp
我在世界各地的互联网上闲逛,现在尤其是 angular.io style docs ,我找到了很多对 @HostBinding 和 @HostListener 的引用。看起来它们非常基础,但不幸的是目
我有一个带有加载、错误显示和实际内容的包装器。根据某些输入,我想显示一个或另一个。 我想测试是否有任何错误添加了 'center__holder' 类 组件: @Component({ selec
我正在使用 @HostBinding 将函数的结果绑定(bind)到宿主元素的可见属性: @HostBinding('attr.visible') private get visibleAttr():
我正在尝试将 CSS 类 foo 绑定(bind)到我的主机组件,使用 @HostBinding 取决于我在动态 变量。但无法使其正常工作。 这是我已经尝试过的: export class MyCom
我有这段代码在主机上设置了一个类: @HostBinding('class.fixed') true; 我想做的是使它成为一个我可以修改的变量类。我该怎么做? 最佳答案 这不能变成可变的。 您可以做的
是否可以像此示例一样绑定(bind) @HostBinding 值? @Input() user: User; @HostBinding("class.temp") user.role == "Adm
我有一个有用户登录和注销功能的 Angular 应用程序。在用户登录之前,我会显示一个欢迎页面作为主页。我只想在欢迎页面上启用背景图像。用户登录后,背景图像必须消失。当用户注销时,他将被重定向到欢迎页
我有一个使用 @HostBinding 设置类的组件: @HostBinding('class.dark-1') true; 效果很好。但是,现在我需要在我的组件中创建一个函数来动态更改类。 例如,当
我使用 @HostBinding 与 Chrome 的拖放 API 一起设置一个类,如下所示: @Directive({ selector: '[sortable-article]' }) expor
我的根本问题是我需要设置一个异步函数 alters an attribute of the host .由于 @HostBinding 没有异步能力,我在组件上设置了一个实例变量,绑定(bind)它,
flex-layout 没有在宿主元素上应用内联样式,宿主元素通过@HostBinding 获取它们的 flex-layout 属性 @Component({ selector: 'colum
这个问题在这里已经有了答案: In RC.1 some styles can't be added using binding syntax (2 个答案) 关闭 6 年前。 使用 @HostBin
问题: 是否可以使用 @HostBinding 来添加、删除或切换宿主元素上的类,而不是删除预先存在的类,特别是当类需要动态切换? 例如,这将添加 light 类并且不会中断之前的类;但是,light
我正在尝试使用 @HostBinding 装饰器在宿主元素上设置 ngIf。 class ListItem { @HostBinding('ngIf') active: boolean = f
我有一个基于 bootstrap3 的模板,我正在向它添加 angular2。 当更改从登录到应用程序其余部分的路由时,我需要更改正文的类,这是我的根组件选择器。 根组件 @Component({
在我的 TSLint 文件中,我有: "no-unused-variable": true 在我的组件中,我有时会: // tslint:disable-next-line:no-unused-var
我是一名优秀的程序员,十分优秀!