【问题标题】:Binding for attribute Angular 2 doesn't update associated input with bound id属性 Angular 2 的绑定不会更新具有绑定 ID 的关联输入
【发布时间】:2017-02-24 23:21:52
【问题描述】:

我的组件中有一个复选框,其绑定如下:

<input type="checkbox" [name]="name" [id]="id" /><label [htmlFor]="id"></label>

当组件加载时,我可以检查元素并发现传递给这些输入的值是绑定的,但是当我单击标签时,复选框不会被选中。

我也试过这样:

<input type="checkbox" [attr.name]="name" [attr.id]="id" /><label [attr.for]="id"></label>
<input type="checkbox" name="{{name}}" id="{{id}} /><label [attr.for]="id"></label>

及其组合。它们都产生相同的效果,数据已绑定,复选框未选中。

这是我的组件:

import { Component } from '@angular/core';

@Component({
  inputs: ['name', 'id'],
  selector: 'my-checkbox',
  templateUrl: 'app/form/input/checkbox.component.html'
})

export class CheckboxComponent {}

以及使用它的html:

<my-checkbox name="test-name" id="test-id"></my-checkbox>

【问题讨论】:

    标签: javascript angular angular2-forms angular2-components


    【解决方案1】:

    尝试以下方法之一:

    <my-checkbox [name]="'test-name'" [id]="'test-id'"></my-checkbox>
    <my-checkbox name="test-name" [id]="'test-id'"></my-checkbox>
    

    您需要将值作为字符串传递,[property]="experession",对于要计算为字符串的表达式,它必须用引号引起来 'test-name'。

    plunker

    【讨论】:

    • 这并不能完全解决问题。 name 和 id 的值在元素上变得未定义。标签行为现在按其应有的方式运行,但只有第一个呈现的复选框的值会发生变化,因为它们都共享相同的 id/for 属性值。组件或其模板是否有相应的变化?
    • 这也是正确的。在许多方面,这似乎是实现这一目标的更可取的方式,因为它们的意图更加清晰:将它们的值绑定到模板。我忽略了绑定值始终是表达式的事实,所以你是正确的,它需要是一个字符串或变量值才能以我期望的方式绑定。
    【解决方案2】:

    以下是否有效?

    &lt;input type="checkbox" name="{{name}}" id="{{id}} /&gt;&lt;label for="{{id}}"&gt;&lt;/label&gt;

    【讨论】:

    • 几分钟前我刚刚更新了我的问题。这不起作用,因为 Angular 会给出一个关于不是标签元素的已知属性的错误。解决方案是使用[htmlFor] 属性或[attr.for] 属性绑定。
    【解决方案3】:

    当您使用id 作为输入名称时,您将组件作为具有此ID 的dom-element。它将相同的 id 添加到复选框和标签的for。但是当您有多个具有相同 id 的元素时,标签指的是其中的第一个。在您的情况下,它指的是您的组件而不是 input。

    只需重命名输入参数即可。

    顺便说一句,在 ts 类中声明相应的字段是对的。

    http://plnkr.co/edit/JazXV3Oi91hSBTdVlBHh?p=preview

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      template: `
          <my-checkbox-broken id="test-id-1"></my-checkbox-broken><br>
          <my-checkbox-broken [id]="'test-id-2'"></my-checkbox-broken> can work too<br>
          <my-checkbox-working chkId="test-id-3"></my-checkbox-working><br>
          <output></output>
      `
    })
    export class AppComponent {
    }
    
    @Component({
      inputs: ['id'],
      selector: 'my-checkbox-broken',
      template: '<input type="checkbox" [id]="id" /><label [htmlFor]="id">Broken :(</label>'
    })
    export class CheckboxComponent_Broken {
      id: string;
    }
    
    @Component({
      inputs: ['chkId'],
      selector: 'my-checkbox-working',
      template: '<input type="checkbox" [id]="chkId" /><label [htmlFor]="chkId">Working!</label>'
    })
    export class CheckboxComponent_Working {
      chkId: string;
    }
    
    document.addEventListener('click', ({ target: { htmlFor: id } }) => {
      if (id) {
        document.querySelector('output').textContent = id + " is " + document.getElementById(id).tagName;
      }
    })
    

    【讨论】:

    • 如此简单。感谢您的答复。 :)
    • @JohnHalbert,这是一个很好的问题 :) 顺便说一句,然后接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    相关资源
    最近更新 更多