Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 Component - How Can i get all inputs inside the content

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?

like image 944
Tim Avatar asked Dec 01 '17 21:12

Tim


People also ask

How do you find the value of input field in a component?

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.

How do you get the data obtained from user actions in angular?

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 ( # ).

How to use @input () in angular?

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.

How do I share data between two components in angular?

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.

How do I watch for changes in an angular component?

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.

How do I pass data into an angular component?

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.


1 Answers

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;
like image 176
Hugo Noro Avatar answered Oct 22 '22 22:10

Hugo Noro