angular - Set attribute inside ngfor in Angular2 -


trying set selected attribute inside ngfor. code below doesn't work because browsers dumb , checked=false still count checked...

so, whatever returned needs true or null. saw guys post *ngif html attribute in angular2 can't work because need pass value loop function.

i tried setting [attr.checked] function click event didn't work either.

template:

<div class="tools">   <div class="radio-group group-{{section.name}}">     <label *ngfor="let content of section.content" class="{{content.name}}">       <input         type="radio"         name="{{section.three}}-{{section.name}}"         value="{{content.id}}"  <!-- problem -->         [attr.checked]="content.id === section.default" <!-- you've pass problem -->          (click)="updatemodel(section, content)" />       <div class="radio">         <span *ngif="content.thumbnail.charat(0) == '#'"             class="image" [style.background-color]="content.thumbnail">         </span>         <span *ngif="content.thumbnail.length > 0 &&                      content.thumbnail.charat(0) != '#'" class="image">           <img src="/thumbnails/{{section.three}}/{{content.thumbnail}}.jpg" alt="" />         </span>         <span *ngif="content.thumbnail == ''" class="image"></span>         <span class="label">{{content.displayname}}</span>       </div>     </label>   </div> </div> 

component:

import { component, input } '@angular/core'; import { aservice } './a.service';  @component({     selector: '[radio]',     templateurl: 'app/newtabs/ui.radio.component.html' }) export class uiradiocomponent {     constructor(private aservice: aservice) {}      @input() section:any;      updatemodel(info: any, content: ) {            //does things not important         }     } } 

you should able set attribute comparing values inline. try:

[attr.checked]="section.default == content.id ? 'checked' : null" 

Comments