Evaluate expression in dynamic UI composition

Hi Team,

I am trying to use dynamic UI composition where I need to show or hide a composed element based on the expression (line number 20 in welcome.ts)in the JSON property. No luck. Please help.

1 Like

I think you’ll need to use eval, like this:

show.two-way="!eval(item.show)"

Although using eval is almost always a bad idea.

1 Like

@jeffgrann,

Thanks for responding, no luck with eval again. Trying with Parser from aurelia-binding, still struggling.

I am referring the below one

1 Like

Hi @ashley ,

I have followed your blog for solving this. DynamicExpressionBindingBehavior saved me.

Thank you.

1 Like

Hi Team,
I am facing another problem, when the expression has a method call it is not updating. Please Help

for example:
show: "questions[2].answer.indexOf(‘日産’) >= 0"

Line 37 below

1 Like

what do you expect to be updated in this scenario? The parameter passed to the method indexOf is a literal string, means it will never change, so it shouldn’t update right?

@bigopon,

Let me share the exact use case and sample code with you for better understanding. Thanks for your support.

1 Like

Hi @bigopon,

Use case:

  1. When first question answer is “red” third question is enabled to answer
  2. third question answer decides 4th question should be enabled for answering or not (here I am struggling).
1 Like

I was able to make it work here https://codesandbox.io/s/nostalgic-khorana-92uc0

There’s 2 key changes:

  • you dont need to rebase the expression, you want to replace it instead
  • you need to observe the array at questions[2].answer, as the mutation of that won’t trigger changes in the expression questions[2].answer.indexOf('Nissan'). To overcome this, I added an noop value converter to basically observe the array questions[2].answer, there’s other way to do it too.

Edit: you can also change the && questions[2].answer && to && questions[2].answer.length && so it can observe everything properly

1 Like

@bigopon, Thank you very much for helping me. Learing new things, day by day.

Thank you,
Praveen Gandhi.

1 Like