如何为angular2中的组件html中的元素触发onload事件

Ken*_*Ken 8 typescript angular2-template angular

所以这是一个二合一的问题.

首先,我试图在组件html中的元素加载时触发函数.我尝试了很多方法,比如:<div [onload]="myFunction()">这会导致函数被多次调用,确切地说是10.我的猜测是这不是要走的路,但我还不够熟悉,无法让它正常工作.另外,我想将元素作为参数发送.例如,做<div #myDiv (click)="myFunction(myDiv)">确实有效,但显然这不会触发所述元素的onload.什么是正确的方式,或者我有义务做一个querySelector ...

接下来是涉及在组件内注入ElementRef的问题.现在,文档告诉我使用'nativeElement'属性不是很好的方法.我真的不明白为什么.在组件中引用元素是一件好事,不是吗?还是我在监督关注事项的分离?我问,因为如果我的第一个问题不是一个选项,我想使用这个元素引用来在OnInit类的ngOnInit函数中执行我想要的onload触发元素的querySelection.

欢迎所有信息,看到angular2文档不完整.谢谢.

export class HomeComponent implements OnInit
{
    public categories: Category[];
    public items: Item[];

    constructor
    (
        public element: ElementRef,
        private _categoryService: CategoryService,
        private _itemService: ItemService,
        private _router: Router
    ){}

    public registerDropdown(element:HTMLElement): void
    {
        console.log(element);
    }

    private getCategories(): void
    {
        this._categoryService.getAll().then((categories:Category[])=> this.categories = categories);
    }

    private getItems(): void
    {
        this._itemService.getAll().then((items:Item[])=> this.items = items);
    }

    public ngOnInit(): any
    {
        this.getCategories();
        this.getItems();
    }
}
Run Code Online (Sandbox Code Playgroud)
<div id="home">

    <div id="search">
        <div class="container">

            <!-- div in question, the [ngModel] is a shot in the dark -->
            <div #myDiv class="dropdown category" [ngModel]="registerDropdown(myDiv)">
                <span class="placeholder">Selecteer categorieën</span>
                <div class="the-drop">
                    <ul class="ul">
                        <li *ngFor="#category of categories">
                            <input type="checkbox" />{{category.long}}
                        </li>
                    </ul>
                </div>
            </div>
          
        </div>
    </div>
  
</div>
Run Code Online (Sandbox Code Playgroud)

Bea*_*alo 9

有关加载事件,请查看本文从新手错误#2开始.

对于一般事件,我发现EventEmitter作为子组件(自定义标记标记)的一种方式可以告诉父组件有关子事件的信息.在子项中,创建一个自定义事件(一个装饰的类变量@Output()),.emit()每当您确定时,它都可以包含您指定的EventEmitter的参数<data type>.然后,父级可以处理自定义事件并访问您捆绑在其中的参数$event.我正在使用Angular 2快速入门文件.

儿童剧本:

import {Component, Output, EventEmitter} from '@angular/core';
@Component({
  selector: 'my-child',
  templateUrl: 'app/my-child.component.html'
})
export class MyChildComponent {
  @Output() childReadyEvent: EventEmitter<string> = new EventEmitter();

  ngOnInit(){
    //do any lines of code to init the child
    console.log("this executes second");
    //then finally,
    this.childReadyEvent.emit("string from child");        
  }
}
Run Code Online (Sandbox Code Playgroud)

父母加价:

<h3>I'm the parent</h3>
<my-child (childReadyEvent)="parentHandlingFcn($event)"></my-child>
Run Code Online (Sandbox Code Playgroud)

脚本:

import {Component} from '@angular/core';
import {MyChildComponent} from './my-child.component';

@Component({
  selector: 'my-parent',
  templateUrl: 'app/my-parent.component.html',
  directives: [MyChildComponent]
})
export class MyParentComponent {

  ngOnInit(){
    console.log("this executes first");
  }

  parentHandlingFcn(receivedParam: string) {
    console.log("this executes third, with " + receivedParam); //string from child
  }

}
Run Code Online (Sandbox Code Playgroud)

注意:您也可以尝试EventEmitter<MyChildComponent>.emit(this)


Sna*_*ops 0

div您可以获取您使用的实例Query,然后在ngOnInit触发registerDropdown方法中并nativeElementQueryList<ElementRef>

export class HomeComponent implements OnInit{
  public categories: Category[];
  public items: Item[];

  constructor
  (
    public element: ElementRef,
    private _categoryService: CategoryService,
    private _itemService: ItemService,
    private _router: Router,
    @Query('myDiv') private elList: QueryList<ElementRef>
  ){}

  public registerDropdown(element:HTMLElement): void
  {
    console.log(element);
  }

  ...

  public ngOnInit(): any
  {
    this.getCategories();
    this.getItems();
    this.registerDropdown(elList.first.nativeElement);
  }
}
Run Code Online (Sandbox Code Playgroud)