QuerySelect Angular Component As MyComponent
I have an angular component
<my-component foo="" bar=""></my-component>
And its corresponding class MyComponent
I use this component in my 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>
Now i want to querySelect my custom elements and access their attributes directly. I think about something like this:
List<MyComponent> mys = querySelector('my-component');
mys.forEach((my){
print(my.foo);
my.bar = '1234';
});
There are a view problems with that code:
querySelector always returns Element not MyComponent. can i cast Element to MyComponent?
Is MyComponent to <my-component> like DivElement to <div>?
querySelector cannot select custom elements. i could ad a class to every my-component and select it with that class. or is there another way?
I only can access the attributes with my.getAttribute() not with my.foo. I know, this is because my is still a Element not a MyComponent.
Answer
This is not officially supported. There was something like ng-element
that allowed this as far as I remember but was intended to be only used for unit tests. There were some changes in the last versions but I don't know the current state.
You should pass references using dependency injection, Angular events (explained here How to communicate between Angular DART controllers) or the scope to access other elements.
Related Questions
- → Filter out all elements in a jQuery result set that have a parent (at any level) with a certain class
- → jQuery selector : 'starts-with && ends-with' doesnt work on element with multiple classes
- → How to loop over elements in a div using .each?
- → Select nth-last-child when not every element has the selecting class-jquery
- → Dynamic jquery selector based on html attribute
- → Get image URL that is inside a <li> that is inside an array of jQuery selector items
- → JQuery variable as ID results in Uncaught Error: Syntax error, unrecognized expression: '#cell32'
- → Uncaught Error: Syntax error, unrecognized expression: select:[id*="devices_0_command"]
- → Select cell by td headers (using variable) and class using javascript
- → Jquery matching values
- → How can I select an element which has only a specific class?
- → How can I retrieve the attribute of a clicked element with jQuery?
- → jquery selector to select all text inputs not of a specified class