description |
---|
Binding expression examples, demonstrating valid syntax and options. |
Aurelia's expression parser implements a subset of ECMAScript Expressions. For the features that are supported, you can typically expect the JavaScript in your view to work the same way as it would in your view-model, or in the browser console. In addition there are two adjustments:
- The Ampersand
&
represents aBindingBehavior
(instead of Bitwise AND) - The Bar
|
represents aValueConverter
(instead of a Bitwise OR)
Non-expression syntax (statements, declarations, function and class definitions) is not supported.
As an overview of various expressions that are possible, the following list is for illustrative purposes and not exhaustive (and not necessarily recommended, either), but should give you a fairly good idea of what you can do.
foo
- Thefoo
variable in the current view-modelßɑṙ
- Theßɑṙ
variable in the current view-model
{% hint style="info" %} Info
non-ASCII characters in the Latin script are supported. This script contains 1,350 characters covering the vast majority of languages. Other Non-BMP characters / Surrogate Pairs are not supported. {% endhint %}
$this
- The current view-model$parent
- The parent view-model
true
- The literal valuetrue
false
- The literal valuefalse
null
- The literal valuenull
undefined
- The literal valueundefined
'foo'
or"foo"
- The literal stringfoo
'\n'
- The literal string[NEWLINE]
'\t'
- The literal string[TAB]
'\''
- The literal string'
'\\'
- The literal string\
'\\n'
- The literal string\n
'\u0061'
- The literal stringa
{% hint style="warning" %} Warning
Unsupported string literals include '\x61'
(2-point hex escape), '\u{61}'
or '\u{000061}'
(n-point braced unicode escape), and Non-BMP characters and Surrogate Pairs.
{% endhint %}
foo
- Equivalent to'foo'
foo\${bar}baz\${qux}quux
- Equivalent to'foo'+bar+'baz'+qux+'quux'
42
- The literal number42
42.
or42.0
- The literal number42.0
.42
or0.42
- The literal number0.42
42.3
- The literal number42.3
10e3
or10E3
- The literal number1000
{% hint style="warning" %} Warning
Unsupported numeric literals include 0b01
(binary integer literal), 0o07
(octal integer literal), and 0x0F
(hex integer literal).
{% endhint %}
[]
- An empty array[1,2,3]
- An array containing the literal numbers1
,2
and3
[foo, bar]
- An array containing the variablesfoo
andbar
[[]]
- An array containing an empty array
{% hint style="warning" %} Warning
Unsupported array literals include [,]
- Elision
{% endhint %}
{}
- An empty object{foo}
or{foo,bar}
- ES6 shorthand notation, equivalent to{'foo':foo}
or{'foo':foo,'bar':bar}
{42:42}
- Equivalent to{'42':42}
{% hint style="warning" %} Warning
Unsupported object literals include {[foo]: bar}
or {['foo']: bar}
(computed property names).
{% endhint %}
foo
here represents any valid primary expression or unary expression.
+foo
or+1
- Equivalent tofoo
or1
(the+
unary operator is always ignored)-foo
or-1
- Equivalent to0-foo
or0-1
!foo
- Negatesfoo
typeof foo
- Returns the primitive type name offoo
void foo
- Evaluatesfoo
and returnsundefined
{% hint style="warning" %} Warning
Unary increment (++foo
or foo++
), decrement (--foo
or foo--
), bitwise (~
), delete
, await
and yield
operators are not supported.
{% endhint %}
a
and b
here represent any valid primary, unary or binary expression.
a*b
ora/b
ora%b
- Multiplicativea+b
ora-b
- Additivea<b
ora>b
ora<=b
ora>=b
ora in b
ora instanceof b
- Relationala==b
ora!=b
ora===b
ora!==b
- Equalitya&&b
- Logical ANDa||b
- Logical OR
{% hint style="warning" %} Warning
Exponentiation (a**b
) and bitwise operators are not supported.
{% endhint %}
foo
etc here represent any valid primary, unary, binary or conditional expression.
foo ? bar : baz
foo ? bar : baz ? qux : quux
foo
here must be an assignable expression (a simple accessor, a member accessor or an indexed member accessor). bar
can any valid primary, unary, binary, conditional or assignment expression.
foo = bar
foo = bar = baz
Member expressions with special meaning in Aurelia:
$parent.foo
- Access thefoo
variable in the parent view-model$parent.$parent.foo
- Access thefoo
variable in the parent's parent view-model$this
- Access the current view-model (equivalent to simplythis
inside the view-model if it's an ES class)
Normal member and call expressions:
foo
here represents any valid member, call, assignment, conditional, binary, unary or primary expression (provided the expression as a whole is also valid JavaScript).
foo.bar
- Member accessorfoo['bar']
- Keyed member accessorfoo()
- Function callfoo.bar()
- Member function callfoo['bar']()
- Keyed member function call
Tagged template literals:
foo
here should be a function that can be called. The string parts of the template are passed as an array to the first argument and the expression parts are passed as consecutive arguments.
- ``foo
bar``` - Equivalent to
foo(['bar'])` - ``foo
bar\${baz}qux``` - Equivalent to
foo(['bar','qux'], baz)` - ``foo
bar\${baz}qux\${quux}corge``` - Equivalent to
foo(['bar','qux','corge'],baz,quux)` - ``foo
\${bar}\${baz}\${qux}``` - Equivalent to
foo(['','','',''],bar,baz,qux)`
These are not considered to be a part of normal expressions and must always come at the end of an expression (though multiple can be chained). Furthermore, BindingBehaviors must come after ValueConverters. (note: BindingBehavior and ValueConverter are abbreviated to BB and VC for readability)
Valid BB expressions:
foo & bar & baz
- Applies the BBbar
to the variablefoo
, and then applies the BBbaz
to the result of that.foo & bar:'baz'
- Applies the BBbar
to the variablefoo
, and passes the literal string'baz'
as an argument to the BBfoo & bar:baz:qux
- Applies the BBbar
to the variablefoo
, and passes the variablesbaz
andqux
as arguments to the BB'foo' & bar
- Applies the BBbar
to the literal string'foo'
Valid VC expressions (likewise):
foo | bar | baz
foo | bar:'baz'
foo | bar:baz:qux
'foo' | bar
Combined BB and VC expressions:
foo | bar & baz
foo | bar:42:43 & baz:'qux':'quux'
foo | bar | baz & qux & quux
Invalid combined BB and VC expressions (BB must come at the end):
foo & bar | baz
foo | bar & baz | qux