Angular @Hostbinding工作原理
时间:2022-07-26
本文章向大家介绍Angular @Hostbinding工作原理,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
使用@HostBinding的一个例子:
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];
}
}
视图的更新检测入口:core.js里的tick()函数:
位于ApplicationRef内:
setHostBindingsByExecutingExpandoInstructions(tView, lView);
/**
* Update a style binding on an element with the provided value.
*
* If the style value is falsy then it will be removed from the element
* (or assigned a different value depending if there are any styles placed
* on the element with `styleMap` or any static styles that are
* present from when the element was created with `styling`).
*
* Note that the styling element is updated as part of `stylingApply`.
*
* @codeGenApi
* @param {?} prop A valid CSS property.
* @param {?} value New value to write (`null` or an empty string to remove).
* @param {?=} suffix Optional suffix. Used with scalar values to add unit such as `px`.
* Note that when a suffix is provided then the underlying sanitizer will
* be ignored.
*
* Note that this will apply the provided style value to the host element if this function is called
* within a host binding function.
*
* @return {?}
*/
function ɵɵstyleProp(prop, value, suffix) {
checkStylingProperty(prop, value, suffix, false);
return ɵɵstyleProp;
}
/**
* Common code between `ɵɵclassProp` and `ɵɵstyleProp`.
*
* @param {?} prop property name.
* @param {?} value binding value.
* @param {?} suffixOrSanitizer suffix or sanitization function
* @param {?} isClassBased `true` if `class` change (`false` if `style`)
* @return {?}
*/
function checkStylingProperty(prop, value, suffixOrSanitizer, isClassBased) {
/** @type {?} */
const lView = getLView();
/** @type {?} */
const tView = getTView();
// Styling instructions use 2 slots per binding.
// 1. one for the value / TStylingKey
// 2. one for the intermittent-value / TStylingRange
/** @type {?} */
const bindingIndex = incrementBindingIndex(2);
if (tView.firstUpdatePass) {
stylingFirstUpdatePass(tView, prop, bindingIndex, isClassBased);
}
if (value !== NO_CHANGE && bindingUpdated(lView, bindingIndex, value)) {
// This is a work around. Once PR#34480 lands the sanitizer is passed explicitly and this line
// can be removed.
/** @type {?} */
let styleSanitizer;
if (suffixOrSanitizer == null) {
if (styleSanitizer = getCurrentStyleSanitizer()) {
suffixOrSanitizer = (/** @type {?} */ (styleSanitizer));
}
}
/** @type {?} */
const tNode = (/** @type {?} */ (tView.data[getSelectedIndex() + HEADER_OFFSET]));
updateStyling(tView, tNode, lView, lView[RENDERER], prop, lView[bindingIndex + 1] = normalizeAndApplySuffixOrSanitizer(value, suffixOrSanitizer), isClassBased, bindingIndex);
}
}
/**
* Update a simple (property name) styling.
*
* This function takes `prop` and updates the DOM to that value. The function takes the binding
* value as well as binding priority into consideration to determine which value should be written
* to DOM. (For example it may be determined that there is a higher priority overwrite which blocks
* the DOM write, or if the value goes to `undefined` a lower priority overwrite may be consulted.)
*
* @param {?} tView Associated `TView.data` contains the linked list of binding priorities.
* @param {?} tNode `TNode` where the binding is located.
* @param {?} lView `LView` contains the values associated with other styling binding at this `TNode`.
* @param {?} renderer Renderer to use if any updates.
* @param {?} prop Either style property name or a class name.
* @param {?} value Either style value for `prop` or `true`/`false` if `prop` is class.
* @param {?} isClassBased `true` if `class` (`false` if `style`)
* @param {?} bindingIndex Binding index of the binding.
* @return {?}
*/
function updateStyling(tView, tNode, lView, renderer, prop, value, isClassBased, bindingIndex) {
if (tNode.type !== 3 /* Element */) {
// It is possible to have styling on non-elements (such as ng-container).
// This is rare, but it does happen. In such a case, just ignore the binding.
return;
}
/** @type {?} */
const tData = tView.data;
/** @type {?} */
const tRange = (/** @type {?} */ (tData[bindingIndex + 1]));
/** @type {?} */
const higherPriorityValue = getTStylingRangeNextDuplicate(tRange) ?
findStylingValue(tData, tNode, lView, prop, getTStylingRangeNext(tRange), isClassBased) :
undefined;
if (!isStylingValuePresent(higherPriorityValue)) {
// We don't have a next duplicate, or we did not find a duplicate value.
if (!isStylingValuePresent(value)) {
// We should delete current value or restore to lower priority value.
if (getTStylingRangePrevDuplicate(tRange)) {
// We have a possible prev duplicate, let's retrieve it.
value = findStylingValue(tData, null, lView, prop, bindingIndex, isClassBased);
}
}
/** @type {?} */
const rNode = (/** @type {?} */ (getNativeByIndex(getSelectedIndex(), lView)));
applyStyling(renderer, isClassBased, rNode, prop, value);
}
}
最重要的platform-browser.js里的setStyle函数:这是HTML element的样式改变最后执行的语句。
- hdu1710(Binary Tree Traversals)(二叉树遍历)
- 基本线程同步(一) 同步方法
- uva514(trail)(模拟栈)
- zoj3822 Domination(概率dp)
- Veeam Backup & Replication(三):创建备份与还原备份
- 使用Go开发一个简单的服务器程序
- C++ 与设计模式学习(其一)
- xz文件压缩工具的用法
- Java 中正确使用 hashCode 和 equals 方法
- C/C++ 关于生成静态库(lib)/动态库(dll)文件如何使用(基于windows基础篇)
- openwrt将LAN口改为WAN方法
- c/c++----网站及其后门(CGI应用程序)
- C/C++深度copy和浅copy
- pycharm2016版汉化方法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- redis妙用-list类型
- redis妙用-set类型
- JVM调优实战:解决CMS concurrent-abortable-preclean LongGC的问题
- redis妙用-zset类型
- 【线上排查实战】AOP切面执行顺序你真的了解吗
- 使用markdown,knitr和pandoc在R语言中编写可重现的报告
- R语言广义线性模型(GLMs)算法和零膨胀模型分析
- R语言中广义线性模型(GLM)中的分布和连接函数分析
- R语言自适应平滑样条回归分析
- R语言区间数据回归分析
- R语言ggsurvplot绘制生存曲线报错 : object of type ‘symbol‘ is not subsettable
- R软件SIR模型网络结构扩散过程模拟
- R语言中使用线性模型、回归决策树自动组合特征因子水平
- R语言缺失值的处理:线性回归模型插补
- R语言如何解决线性混合模型中畸形拟合(Singular fit)的问题