试图在一个内部设置所选属性ngFor.下面的代码不起作用,因为浏览器是愚蠢的,checked=false仍然会被视为已检查...
因此,返回的内容必须为true或null.我看到这些人在Angular2中发布了*ngIf的html属性,但我无法使用它,因为我需要将一个值从for循环传递给函数.
我也试过设置[attr.checked]像click事件这样的函数但是也没用.
模板:
<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}}"
<!-- This is my problem -->
[attr.checked]="content.id === section.default"
<!-- You've just pass the 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>
Run Code Online (Sandbox Code Playgroud)
零件:
import { Component, Input } from '@angular/core';
import { AService } from './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: any ) {
//does things but not important
}
}
}
Run Code Online (Sandbox Code Playgroud)
您应该能够通过简单地比较内联值来设置属性.尝试:
[attr.checked]="section.default == content.id ? 'checked' : null"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2334 次 |
| 最近记录: |