Posted onEdited onInangular Symbols count in article: 1.5kReading time ≈6 mins.
What’s Signal?
先看一下官方定义:
1
A signal is a wrapper around a value that notifies interested consumers when that value changes. Signals can contain any value, from primitives to complex data structures.
import _ from'lodash'; const data = signal(['test'], {equal: _.isEqual}); // Even though this is a different array instance, the deep equality // function will consider the values to be equal, and the signal won't // trigger any updates. data.set(['test']);
Note that, for simple values, typescript can infer the type by value, but for complex types, you need to specify the type.
1 2
value = input<number>(0); // value = input(0); // totally ok value = input<string>(''); // value = input(''); // totally ok
required
1
value = input.required<number>(); // required
Input transform
1 2 3 4 5 6 7 8 9
exportclassListItemComponent { id = input.required<string>(); name = input('', {transform: trimString}); // apply transform }
// define transform function, transform function should be statically analyzable and pure function. functiontrimString(value: string | undefined): string { return value?.trim() ?? ''; }
Built-in transform
booleanAttribute - imitates the behavior of standard HTML boolean attributes, where the presence of the attribute indicates a “true” value. However, Angular’s booleanAttribute treats the literal string “false” as the boolean false.
numberAttribute - attempts to parse the given value to a number, producing NaN if parsing fails.
Input alias
1
value = input(0, {alias: 'sliderValue'}); // aliasValue is the alias name
Posted onEdited onInangular Symbols count in article: 857Reading time ≈3 mins.
这篇探讨一下Angular中::ng-deep伪类的用法。
::ng-deep是什么?
以下是::ng-deep的官方描述:
(deprecated) /deep/, >>>, and ::ng-deep Component styles normally apply only to the HTML in the component’s own template.
Applying the ::ng-deep pseudo-class to any CSS rule completely disables view-encapsulation for that rule. Any style with ::ng-deep applied becomes a global style. In order to scope the specified style to the current component and all its descendants, be sure to include the :host selector before ::ng-deep. If the ::ng-deep combinator is used without the :host pseudo-class selector, the style can bleed into other components.