【发布时间】:2018-11-13 20:46:15
【问题描述】:
我正在构建一个 Angular 6 应用程序,我想将 select 的显示/隐藏逻辑绑定到复选框 input。我不确定我的问题在哪里。我想我需要一个可观察的,但我想知道是否有办法直接做到这一点(直接就像在我的打字稿代码中不使用变量一样)。
import { Component, Input } from '@angular/core';
@Component({
selector: 'hello',
template: `<label><input #bl id="checkBox" type="checkbox"> ere</label>
<br/>
<span>{{bl.checked}}</span>
<br/>
<select id="bl_select" class="select" *ngIf="(bl.checked)">
<option value="0">All</option>
<option value="1">Else</option>
</select>`,
styles: []
})
export class HelloComponent {
}
您可以在stackblitz.com 上找到MCVE。
与 How do I bind a checkbox to a select disabled property using angular data binding? 相关,但与 ngx 相关。
在我的 wpf 时代,这是数据绑定的常见用途 :)
【问题讨论】:
-
@ConnorsFan 它正在工作,但不是直接工作。我们依赖于一个外部变量。可以直接绑定吗?
-
我不这么认为。正如您在 stackblitz 中看到的,如果没有 Angular 数据绑定或事件绑定,就不会执行更改检测。
标签: javascript angular data-binding web-component