SAP Spartacus 自定义指令的实现以及通过@HostBinding实现属性绑定

简介: SAP Spartacus 自定义指令的实现以及通过@HostBinding实现属性绑定

image.png

import { Directive, HostBinding, HostListener } from '@angular/core';
@Directive({
  selector: '[appRainbow]'
})
export class RainbowDirective{
  possibleColors = [
    'darksalmon', 'hotpink', 'lightskyblue', 'goldenrod', 'peachpuff',
    'mediumspringgreen', 'cornflowerblue', 'blanchedalmond', 'lightslategrey'
  ];
  @HostBinding('style.color') color: string;
  @HostBinding('style.borderColor') borderColor: string;
  @HostListener('keydown') onKeydown(){
    const colorPick = Math.floor(Math.random() * this.possibleColors.length);
    console.log('Jerry colorPick: ' + colorPick);
    this.color = this.borderColor = this.possibleColors[colorPick];
  }
}

定义一个input field:

<input appRainbow/>

在directive实现内部,通过@HostBinding达到directive施加的host元素的css样式和directive属性绑定的目的。image.png

directive施加的host元素一旦发生keydown事件,会自动触发directive实现的onKeydown函数,每触发一次,修改color和borderColor属性的值,达到host元素变色的效果:

image.png

2021-1-12 星期二

经过测试,使用Directive不需要中括号:image.png

相关文章
|
2天前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
33 0
|
2天前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
18 0
|
2天前
|
XML 存储 数据格式
SAP UI5 控件 customData 属性的应用介绍
SAP UI5 控件 customData 属性的应用介绍
37 0
|
1天前
|
JavaScript 开发者 容器
Spartacus cxConfiguratorAttributeComponent 自定义指令的使用场合
Spartacus cxConfiguratorAttributeComponent 自定义指令的使用场合
6 2
|
1天前
|
存储 供应链
什么是 SAP 产品主数据的 Scale 属性
什么是 SAP 产品主数据的 Scale 属性
6 1
|
2天前
|
开发者 UED
关于 SAP UI5 sap.m.Column 的 demandPopin 属性
关于 SAP UI5 sap.m.Column 的 demandPopin 属性
17 0
|
2天前
|
存储 搜索推荐 BI
sap.ui.comp.filterbar.FilterBar 的 persistencyKey 属性的作用介绍
sap.ui.comp.filterbar.FilterBar 的 persistencyKey 属性的作用介绍
10 0
|
2天前
|
存储 对象存储 开发者
SAP CRM attachment 存储模型的技术属性详解
SAP CRM attachment 存储模型的技术属性详解
21 1
SAP CRM attachment 存储模型的技术属性详解
|
2天前
|
JSON 开发者 数据格式
关于 SAP Spartacus LandingPage2Template 区域的 layout 设计实现
关于 SAP Spartacus LandingPage2Template 区域的 layout 设计实现
28 0
|
2天前
|
搜索推荐 开发者 UED
关于 SAP Spartacus 层的 UI 设计
关于 SAP Spartacus 层的 UI 设计
43 0