user3428555
user3428555

Reputation: 385

Angular 2 beta – Select not working (except in Chrome)

I just have a simple question regarding select element. I spent quite some time debugging my code, because select element did not pick up changes. But then I found out something interesting. They have form example on their page angualr.io: https://angular.io/resources/live-examples/forms/ts/plnkr.html. If you try this example in browser other than Chrome, like Firefox, Edge, IE11 – the select element doesn’t detect changes. Have anyone else notice this problem or I’m missing something? Because change detection in dropdown element like Select is a basec thing … I just cannot believe it doesn’t work.

Upvotes: 4

Views: 2054

Answers (3)

Thierry Templier
Thierry Templier

Reputation: 202146

I think that this answer could provide you a work around and the Mark's answer:

Here is some code:

@Component({
  (...)
  template: `
    <select [ngModel]="selectedDevice" (ngModelChange)="onChange($event)">
      <option *ngFor="#i of devices">{{i}}</option>
    </select>
  `)
  export class SomeComponent {

    onChange(newValue) {
      console.log(newValue);
      this.selectedDevice = newValue;
      // ... do other stuff here ...
    }
  }

Upvotes: 3

bas
bas

Reputation: 1070

This issue is also discussed at the angular github

Adding the shims Angular uses for unit testing its framework (before all other scripts) helps solve a lot of IE issues: https://github.com/angular/angular/blob/master/modules/angular2/src/testing/shims_for_IE.js

This particular case is solved by this particular function.

if (!Object.hasOwnProperty('name')) {
    Object.defineProperty(Function.prototype, 'name', {
        get: function() {
            var matches = this.toString().match(/^\s*function\s*(\S*)\s*\(/);
            var name = matches && matches.length > 1 ? matches[1] : "";
            // For better performance only parse once, and then cache the
            // result through a new accessor for repeated access.
            Object.defineProperty(this, 'name', {value: name});
            return name;
        }
    });
}

Upvotes: 0

Related Questions