简体   繁体   English

(onChange) 不适用于 Dropbox 中的 Angular4 选择

[英](onChange) not working for Angular4 select in dropbox

new to angular 4 and i'm building something. angular 4 的新手,我正在构建一些东西。

I have a couple components, one of them has this as its html:我有几个组件,其中一个将其作为 html:

<div class="row" style="border:1px solid black;">
<br>
<div class="col s1"><h2 class="titleClass">Categories: </h2></div>
    <div *ngFor="let number of numbersList"> 
    <div class="input-field col s2" *ngIf="number < numberOfCategoriesShown">
        <select id={{number}} (onChange)="onChange()" >
            <option value={{allString}}></option>
            <option value={{type}} *ngFor="let type of categoryList[number]">{{type}}</option>
        </select>
        <label>Category {{number + 1}}</label>
    </div>

    </div>
</div>

<button (click)="onChange()"></button>

I want a function to be called whenever I change a value in one of those select objects.我希望每当我更改这些选择对象之一中的值时调用一个函数。 The function is called onChange.该函数称为 onChange。

However, for whatever reason, it's not working.但是,无论出于何种原因,它都不起作用。 But the (click)="onChange()" at the bottom is.但是底部的 (click)="onChange()" 是。 Also funnily enough, if I change the value of the comboBox then try clicking the button, then the button doesn't work either.同样有趣的是,如果我更改了 comboBox 的值然后尝试单击该按钮,则该按钮也不起作用。

Could anyone help me please?有人可以帮我吗? I've decleared the onChange function inside the component's class.我已经在组件的类中清除了 onChange 函数。

Here is what my .ts file looks like:这是我的 .ts 文件的样子:

  import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';
import { VideoService } from '../../services/video.service';
import { Video } from '../../../assets/video';

@Component({
  selector: 'app-filtering',
  templateUrl: './filtering.component.html',
  styleUrls: ['./filtering.component.css'],
  providers: [VideoService]
})
export class FilteringComponent implements OnInit, OnChanges {

  @Input() 
  changeDetection;

  categoryList: Set<String>[] = new Array();
  numbersList: number[] = new Array();
  tagSet  = new Set<String>();
  allString = 'ALL';
  numberOfCategoriesShown = 10;
  selectedItem;

  ngOnChanges(changes: SimpleChanges): void {
    console.log(JSON.stringify(changes)); 
  }

  constructor(private videoService: VideoService) { 

    for (let i = 0; i < this.videoService.getVideos().length; i++) {
      const categoryTypes = this.videoService.getVideos()[i].category.split('->');
      for (let j = 0; j < categoryTypes.length ; j++) {
        if (this.categoryList.length <= j) {
          this.categoryList.push(new Set());
          this.numbersList.push(this.categoryList.length - 1);
        }
        this.categoryList[j].add(categoryTypes[j]);
      }

      this.tagSet.add(this.videoService.getVideos()[i].tags);
    }
  }

  ngOnInit() {

  }

  onChange(newValue) {
    console.log('woop'); 
  }

}

There is no onChange, it is (change)没有 onChange,它是(change)

  <select id={{number}} (change)="onChange()" >

Proper way is to use ngModelChange正确的方法是使用ngModelChange

<select id={{number}}   [(ngModel)]="selectedItem" (ngModelChange)="onChange()" >
            <option value={{allString}}></option>
            <option value={{type}} *ngFor="let type of categoryList[number]">{{type}}</option>
 </select>

有趣的是,当我使用 materialize css 框架时,(change) 不起作用,但如果我不使用它,它确实起作用。

As @Sajeetharan correctly points, your use of onChange is incorrect.正如@Sajeetharan 正确指出的那样,您对onChange的使用是不正确的。

If using the standard HTML controls, the event you want is change .如果使用标准 HTML 控件,您想要的事件是change But if you're using Angular Material (your own answer hints to that), you need to use selectionChange instead of change .但是,如果您使用的是Angular Material (您自己的答案提示),则需要使用selectionChange而不是change Checkout the API here此处查看 API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM