Skip to content

Angular test/conditional API

Angular 核心 API —— @Component 装饰器和生命周期、用于 HTTP 的 HttpClient,以及用于导航的 Router。

1 class · 8 methods

Component

8 methods

@Component 装饰器及相关 API,用于定义组件、输入/输出和生命周期钩子。

[ -f file ]

将类标记为 Angular 组件并附加元数据(selector、template、styles)的装饰器。

Parameters

NameTypeDescription
filepathPath to test.

Returns

boolean (exit status)

Example

bash
if [ -f /etc/hosts ]; then
    echo "regular file exists"
fi
[ -d dir ]

将类属性标记为来自父组件模板的数据绑定输入。

Parameters

NameTypeDescription
dirpathPath to test.

Returns

boolean (exit status)

Example

bash
if [ -d /tmp ]; then
    echo "directory exists"
fi
[ -z string ]

将属性标记为输出,父组件可通过事件绑定进行绑定。

Parameters

NameTypeDescription
stringstringString to test.

Returns

boolean (exit status)

Example

bash
name=""
if [ -z "$name" ]; then
    echo "name is empty"
fi
[ -n string ]

在 Angular 初始化指令的数据绑定属性后调用一次的生命周期钩子。

Parameters

NameTypeDescription
stringstringString to test.

Returns

boolean (exit status)

Example

bash
name="hello"
if [ -n "$name" ]; then
    echo "name is set"
fi
[ str1 = str2 ]

在 Angular 销毁指令前调用一次的生命周期钩子。用于清理。

Parameters

NameTypeDescription
str1stringFirst string.
str2stringSecond string.

Returns

boolean (exit status)

Example

bash
if [ "$USER" = "root" ]; then
    echo "running as root"
fi
if [ "$a" != "$b" ]; then
    echo "different"
fi
[ num1 -eq num2 ]

Returns true if the two integers are equal. Other numeric ops: -ne, -lt, -le, -gt, -ge.

Parameters

NameTypeDescription
num1integerFirst integer.
num2integerSecond integer.

Returns

boolean (exit status)

Example

bash
count=5
if [ $count -eq 5 ]; then echo "five"; fi
if [ $count -gt 3 ]; then echo "greater than 3"; fi
if [ $count -le 5 ]; then echo "less or equal 5"; fi
[ -e path ]

Returns true if path exists (any type: file, directory, symlink, etc.).

Parameters

NameTypeDescription
pathpathPath to test.

Returns

boolean (exit status)

Example

bash
if [ -e /usr/bin/python3 ]; then
    echo "python3 exists"
fi
[[ str =~ regex ]]

Returns true if str matches the extended regular expression regex. Only in [[ ]], not [ ].

Parameters

NameTypeDescription
strstringString to test.
regexregexExtended regular expression.

Returns

boolean (exit status)

Example

bash
phone="123-456-7890"
if [[ $phone =~ ^[0-9]{3}-[0-9]{3}-[0-9]{4}$ ]]; then
    echo "valid phone"
fi
# Capture groups: ${BASH_REMATCH[1]}, etc.