9

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

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

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

欢迎所有信息,看到 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();
    }
}
<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>

4

3 回答 3

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");        
  }
}

标记:

<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)

于 2016-05-25T13:36:49.327 回答
0

在进一步阅读中,似乎实现 Spy 是实现这一目标的最佳方式:

https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html#!#spy

于 2016-05-19T11:19:56.290 回答
0

您可以获取您divusing的实例,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);
  }
}
于 2016-02-10T23:08:30.007 回答