我有一个角度分量
<my-component foo="" bar=""></my-component>
及其对应的类 MyComponent
我在html中使用这个组件
<my-component foo="bar" bar="foo"></my-component>
<my-component foo="baz" bar="qux"></my-component>
<my-component foo="bar" bar="baz"></my-component>
现在我想查询选择我的自定义元素并直接访问它们的属性。我想到这样的事情:
List<MyComponent> mys = querySelector('my-component');
mys.forEach((my){
print(my.foo);
my.bar = '1234';
});
该代码存在一些问题:
querySelector始终返回元素而不是 MyComponent 。我可以将元素转换为MyComponent吗?
MyComponent是&lt; my-component&gt;比如DivElement to&lt; div&gt;?
querySelector无法选择自定义元素。我可以为每个my-component添加一个类,并使用该类选择它。还是有另一种方式?
我只能使用my.getAttribute()而不是my.foo来访问属性。我知道,这是因为我仍然是一个元素而不是一个MyComponent。
答案 0 :(得分:1)
这不是官方支持的。据我所知,有ng-element
这样的东西允许这样做,但是它只用于单元测试。最后一个版本有一些变化,但我不知道当前的状态。
您应该使用依赖注入,Angular事件(此处解释为How to communicate between Angular DART controllers)或访问其他元素的范围来传递引用。