4

In angular 2, it possible to set values within the html view of a component like this? It's already rendered in PHP:

//html page:
<dropdown [(options)]="['Available', 'Busy', 'Away', 'Offline']"></dropdown>

I have never seen this done anywhere online, so I assume it can't be done. But is there a workaround? For example if I had 100 dropdown components on the page with their own options model, I'd potentially have to make 100 requests to the server if doing it the angular way. Thoughts?

1 Answer 1

2
 //html page:
 <dropdown [options]="['Available', 'Busy', 'Away', 'Offline']"></dropdown>

  • This template is perfectly OK for angular, as long as you use it for one-way binding using [prop]="expression" or prop="{{expression}}",

    WORKING PLUNKER


  • because the moment you try to do two-way binding [(prop)]="expression" over an Array Value it will fail, that's because expression must be a component property to do two-way binding.

    It will throw a Teplate parse error, to be precise.

    BROKEN PLUNKER

Sign up to request clarification or add additional context in comments.

3 Comments

Wonderfully explained answer. Thank you for taking the time to answer my question.
here is a similar issue for setting values on root elements which is worth referencing: github.com/angular/angular/issues/1858. Basically this question is going against best practice.
Getting the attributes from html: stackoverflow.com/questions/33326376/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.