代码之家  ›  专栏  ›  技术社区  ›  Alexander Taylor yunyi hu

在角度元素中设置非字符串属性/输入

  •  0
  • Alexander Taylor yunyi hu  · 技术社区  · 8 年前

    可以通过HTML中的属性向角度元素的实例提供输入:

    <some-custom-element someArg="test value"><some-custom-element>
    

    或者使用 setAttribute .

    但属性只能是字符串。

    有没有方法传递非字符串,甚至是javascript对象?也许我可以使用自定义元素直接从应用程序获取组件类的实例?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Alexander Taylor yunyi hu    8 年前

    注意 setAttribute What is the difference between properties and attributes in HTML?

    HTMLElement NgElement

    console.dir(document.querySelector('some-custom-element'));
    

    import {FooElement} from '...';
    
    const myElement = document.querySelector('some-custom-element')! as HTMLElement & FooElement;
    
    // Number instead of string!
    myElement.foo = 5;
    

    @Component({
      ...
    })
    export class FooElement {
      @Input() foo = 0;
    }
    

    至少这对组件的属性有效。我还没有测试方法是否也暴露在外,我也不确定EventEmitter如何在元素中工作。

    推荐文章