【问题标题】:angular2 event not firingangular2事件未触发
【发布时间】:2016-02-24 09:10:26
【问题描述】:

我正在使用 angular2,我有一个下拉列表,并且我有一个要在 (change) 事件上调用的函数。但是这个事件没有触发。以下是我的代码

    import {Component, EventEmitter, OnInit} from 'angular2/core';
    import {perPageCount} from './interface';
    import {CORE_DIRECTIVES} from 'angular2/common';
    @Component({
    selector: 'table-content',
    template: 
     `<div class="dropdown-wrap ">
          <select name="" tabindex="1" (change)="onSelect($event.target.value)">
                  <option *ngFor="#perPageCount of perPageCounts; #i=index" value="{{perPageCount.value}}" [attr.selected]="i == 0 ? true : null" > 
                                    {{perPageCount.text}} 
                  </option>
          </select>
     </div>`,
    providers: [CORE_DIRECTIVES]
})
export class tablecontent {
    public perPageCounts: perPageCount[] = [
        { text: 10, value: 1 },
        { text: 25, value: 2 },
        { text: 50, value: 3 },
        { text: 100, value: 4 }
    ];
    public ipp: number = this.perPageCounts[0].text;

    onSelect(value) {
        this.ipp = null;
        for (var i = 0; i < this.perPageCounts.length; i++) {
            if (this.perPageCounts[i].value == value) {
                this.ipp = this.perPageCounts[i].text;
                console.log(this.ipp)
        }
    }
}

下拉列表正在加载,但onSelect(value) 函数未触发!请有人帮忙!!

【问题讨论】:

  • 你使用哪个版本的 Angular?
  • @ThierryTemplier 2.0.0-beta.7
  • 谢谢!为什么要将 CORE_DIRECTIVES 设置为 providers 属性?
  • 其实我是在做一个试错法..我也把CORE_DIRECTIVES放在指令属性中..但显示相同的结果
  • 事实上,你不再需要设置它们了 ;-)

标签: angular angular2-directives


【解决方案1】:

我更新了一些你的代码,因为我有 TypeScript 错误,但它对我有用。调用onSelect 方法:

import {Component, EventEmitter, OnInit} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';

class perPageCount {
  constructor(public text: number, public value: number) { }
}

@Component({
  selector: 'first-app',
  template:
    `<div class="dropdown-wrap ">
      <select name="" tabindex="1" (change)="onSelect($event.target.value)">
        <option *ngFor="#perPageCount of perPageCounts; #i=index" value="{{perPageCount.value}}" [attr.selected]="i == 0 ? true : null" > 
          {{perPageCount.text}} 
        </option>
      </select>
    </div>`
  })
export class AppComponent {
  public perPageCounts: perPageCount[] = [
    new perPageCount(10, 1),
    new perPageCount(25, 2),
    new perPageCount(50, 3),
    new perPageCount(100, 4)
  ];
  public ipp: number = this.perPageCounts[0].text;

  onSelect(value) {
    this.ipp = null;
    for (var i = 0; i < this.perPageCounts.length; i++) {
      if (this.perPageCounts[i].value == value) {
        this.ipp = this.perPageCounts[i].text;
        console.log(this.ipp)
      }
    }
  }
}

我也像这样测试了你的代码,并且我的行为相同(调用了onSelect 方法)...

也许您忘记在您的 HTML 页面中包含 Rx.js 文件:

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script> <-------
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>

【讨论】:

  • 您的 HTML 页面中是否包含 Rx.js 库?
  • 我解决了这个问题。实际上问题与 Jquery UI 有关。一个名为 createDropdown() 的函数已在页面加载时从 jq 触发,该函数重写了 HTML 代码,并且我的 onSelect() 函数被隐藏。非常感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 2018-11-23
  • 2016-08-09
  • 2018-01-08
  • 1970-01-01
  • 2010-11-08
  • 2019-01-31
相关资源
最近更新 更多