所以这是一个二合一的问题。
首先,我尝试在组件html中的元素加载时触发函数。我尝试了很多方法,例如:<div [onload]="myFunction()">
这会导致函数被多次调用,确切地说是10。我的猜测是这不是要走的路,但我还不够熟悉,无法让它正常工作。另外,我想将元素作为参数发送。例如,执行<div #myDiv (click)="myFunction(myDiv)">
确实有效,但显然这并未触发所述元素的onload。什么是正确的方式,或者我有义务做一个querySelector ...
接下来是涉及在组件内注入ElementRef的问题。现在,the docs告诉我使用&#39; nativeElement&#39;物业不是很好的方式去。我真的不明白为什么。在组件中引用元素是一件好事,不是吗?还是我在监督关注事项的分离?我问,因为如果我的第一个问题不是一个选项,我想使用这个元素引用来在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();
}
}
&#13;
<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>
&#13;
答案 0 :(得分:9)
要加载活动,请从新手错误#2开始查看this article。
对于一般事件,我发现EventEmitter
可用作子组件(自定义标记标记)的方式,以告知父组件有关子事件的事件。在子项中,创建一个自定义事件(一个用@Output()
修饰的类变量),每当您确定时,它将.emit()
,并且可以包含指定<data type>
的EventEmitter的参数。然后,父级可以处理自定义事件并访问您在$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");
}
}
家长加价:
<h3>I'm the parent</h3>
<my-child (childReadyEvent)="parentHandlingFcn($event)"></my-child>
父脚本:
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
}
}
注意:您还可以使用EventEmitter<MyChildComponent>
.emit(this)
答案 1 :(得分:0)
您可以使用div
获取Query
的实例,然后在ngOnInit
中触发registerDropdown
方法,并从nativeElement
传入QueryList<ElementRef>
{1}}
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);
}
}
答案 2 :(得分:0)
进一步阅读,实施间谍似乎是实现这一目标的最佳方式:
https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html#!#spy