Bindable properties on component do not work

I’ve simply copied the exact example here :
TYPESCRIPT:

import { bindable } from ‘aurelia’;
export class NameComponent {
@bindable firstName = ‘’;
@bindable lastName = ‘’;
}
HTML:

Hello ${firstName} ${lastName}. How are you today?

and then insert it into the my-app.html:

<name-component firstName.bind='John' lastName.bind='Smith'></name-component>

Both the properties are simply undefined.

NOTE: I have seen this question and it doesn’t solve my problem whatsoever:

Neither does this one:

Hi, could you please review the code parts that you use in your question? Seems like the text is not escaped properly and therefore not visible. Thanks in advance!

Edited.
Thanks for the response.

Just checking if it’s as simple as changing the bindings? Should they be:

<name-component first-name.bind='John' last-name.bind='Smith'></name-component>

1 Like

I agree, see documentation here, in the first remark: Attribute binding | The Aurelia 2 Docs

Thanks for the response, it got me in the right direction!
I was/am referring to passing in bindable property values into components as on this page:
https://docs.aurelia.io/components/bindable-properties
Using the given syntax now works as in :
<drop-down key-value="id" display-member="description" list-of-values.bind="someArray"></drop-down>
I struggled to get the value of the list-of-values right. I got this error when the aurelia system was trying to create the <select> element using the repeat.for statement:
<option repeat.for="item of listOfValues"
ERROR:
Uncaught (in promise) Error: AUR0777: Unsupported: [repeat] cannot iterate over [object String]

The answer is that to pass a simple string value one uses the syntax without the .bind but when passing a variable inside the model using the component one has to use the .bind syntax.
So my component is then declared as follows:
<drop-down key-member="rnisDmcCode" display-member="fgDmcName" list-of-values.bind="dmcAreas"></drop-down>
where the dmcAreas is an array containing objects which has rnisDmcCode as key and fgDmcName as display value.
Thanks for the support. Much appreciated.

2 Likes

.bind expects a property. So <name-component first-name.bind='John'> Means that Aurelia expects a property John with a value. So what you needed here is <name-component first-name.bind = "'John'"> to pass it a string. Or just <name-component first-name="John"> without .bind it won’t expect a property and you can pass in a string directly.
And yes, the camelCased bindable firstName should be written as kebab-case in the template.

1 Like