Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich is the best way to choose a template based on a ng-model property?
    primarykey
    data
    text
    <p>Let's say we have a person object with a few "person-properties"</p> <pre><code>personModel = { membership-number : "abcd", name : "Anonymous", dob : "14/04/2009", avatar : "image.png" } </code></pre> <p>Let's also say we have metadata about these "person-properties".</p> <pre><code>meta-data = { membership-number: {data-type : "string", editable : "false"}, name : {data-type : "string", editable : "true"}, dob : {data-type : "date", editable : "true"}, avatar : {data-type : "image", editable : "true"} } </code></pre> <p>Now I would like to list these properties of the personModel on a page with the following constraints. member-ship name which is string and non-editable, should be displayed as a label. All editable strings as text box, all date types with a date-picker, etc.</p> <p>I know this can be done with angular directives. Have a directive called person which will iterate through the "person-properties" of the personObject.</p> <pre><code>&lt;person ng-model={{personModel}}&gt; &lt;/person&gt; </code></pre> <p>and have a directive for each "person-property" inside the person, like : </p> <pre><code>&lt;person-property ng-repeat = {{prop in person-properties}}&gt; &lt;/person-property&gt; </code></pre> <p>Let's also assume that the property directive has access to the metadata. To get templates based on the property-type, I can write if-else in the person-property's definition to choose which template to return, like this skeleton.</p> <pre><code>if(property_type === "date"){ template = "&lt;datepicker&gt; &lt;/datepicker&gt;" } else if (property-type === "numeric"){ template = "&lt;numeric-editable-box&gt; &lt;/numeric-editable-box&gt;" } </code></pre> <p>But is there a more elegant way to do this?</p> <p>WPF has something called TemplateSelector to do this. Does angular have something similar that I am unaware of?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload