Im working on a component and i'd like to implement a function that focus's the first input element inside it's ng-content.
So i tried rougly this he following template code and attempted to use ViewChildren and ContentChildren.
@Component({
selector: '[my-component]', // tslint:disable-line
template: `<div class="my-container">
<ng-content></ng-content>
</div>`
})
export class MyComponent implements AfterViewInit, OnDestroy {
@ViewChildren('input') vc;
@ContentChildren('input', {descendants: true}) cc;
ngAfterViewInit() {
//Nothing in either QueryList ???
console.log(this.vc, this.cc);
}
focus () {
//Nothing in either QueryList ???
this.vc.first.nativeElement.focus();
}
}
When use the component in a template/page it then looks roughly like this:
<div my-component>
<div class="field"><input></div>
<div class="field"><input></div>
<div class="field"><input></div>
<div class="field"><input></div>
<div class="field"><input></div>
</div>
So again, what i would like is to get all of the input elements that appear in the content and and they could also be buried be in divs and wrappers. It seems like ContentChildren should be correct but it is not returning me any elements. i.e. QueryList.results is empty.
What am i doing wrong or how can i get the inputs inside the content area?
Answer: Use the value Property You can simply use the value property of the DOM input element to get the value of text input field. The following example will display the entered text in the input field on button click using JavaScript.
There's another way to get the user data: Use Angular template reference variables. These variables provide direct access to an element from within the template. To declare a template reference variable, precede an identifier with a hash (or pound) character ( # ).
Use the @Input() decorator in a child component or directive to let Angular know that a property in that component can receive its value from its parent component. It helps to remember that the data flow is from the perspective of the child component. So an @Input() allows data to be input into the child component from the parent component.
Using @Input () and @Output () together. A common pattern in Angular is sharing data between a parent component and one or more child components. You can implement this pattern by using the @ Input () and @ Output () directives.
To watch for changes on an @ Input () property, you can use OnChanges, one of Angular's lifecycle hooks . See the OnChanges section of the Lifecycle Hooks guide for more details and examples. The @ Output () decorator in a child component or directive allows data to flow from the child to the parent.
In this article, we’re going to explore how to pass data into an Angular component and understand the basis of component architecture. To pass data into an Angular component, we need to be aware of a concept called property binding, which we learned about in the previous article when we bound to an <input> element to display a count.
The @ViewChildren decorator supports both directive or component types as parameter. Supports also the name of a template variable, meaning the #ref definition. So I believe it would work if you put for instance
<div my-component>
<div class="field"><input #myInput></div>
<div class="field"><input #myInput></div>
<div class="field"><input #myInput></div>
<div class="field"><input #myInput></div>
<div class="field"><input #myInput></div>
</div>
And then
@ViewChildren('myInput') vc;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With