0

I had an app running on Angular2 beta-15, but then I updated it to beta-17 and now there is an error hard to decipher.

EXCEPTION: Can only add to a TokenMap! Token: ElementRef angular2.dev.js:25654
EXCEPTION: Can only add to a TokenMap! Token: ElementRef angular2.dev.js:25644:9

Object { message: "Can only add to a TokenMap! Token: …", stack: "BaseException@http://localhost:3000…" } boot.js:25:107

EXCEPTION: Error: Uncaught (in promise): Can only add to a TokenMap! Token: ElementRef angular2.dev.js:25654
EXCEPTION: Error: Uncaught (in promise): Can only add to a TokenMap! Token: ElementRef angular2.dev.js:25644:9

STACKTRACE: angular2.dev.js:25644:9

resolvePromise@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:564:32
scheduleResolveOrReject/<@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:600:18
Zone</ZoneDelegate</ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:382:24
NgZoneImpl/this.inner<.onInvokeTask@http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:2181:22
Zone</ZoneDelegate</ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:381:24
Zone</Zone</Zone.prototype.runTask@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:282:29
drainMicroTaskQueue@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:500:26
ZoneTask/this.invoke@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:452:22
 angular2.dev.js:25644:9

Unhandled Promise rejection: Can only add to a TokenMap! Token: ElementRef ; Zone: angular ; Task: Promise.then ; Value: Object { message: "Can only add to a TokenMap! Token: …", stack: "BaseException@http://localhost:3000…" } angular2-polyfills.js:487:14

Error: Uncaught (in promise): Can only add to a TokenMap! Token: ElementRef
Stack trace:
resolvePromise@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:564:32
scheduleResolveOrReject/<@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:600:18
Zone</ZoneDelegate</ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:382:24
NgZoneImpl/this.inner<.onInvokeTask@http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:2181:22
Zone</ZoneDelegate</ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:381:24
Zone</Zone</Zone.prototype.runTask@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:282:29
drainMicroTaskQueue@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:500:26
ZoneTask/this.invoke@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:452:22
 angular2-polyfills.js:489:10
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
Sergio
  • 3,317
  • 5
  • 32
  • 51
  • 1
    Had a similar error regarding `ElementRef`. Had to switch it out for a `ViewContainerRef`. You should check your code for occurrences of `ElementRef`. Constructors / Functions which handle with it might have changed regarding their params. – malifa May 02 '16 at 22:11

1 Answers1

0

If you are using DynamicComponentLoader usage has changed to

export class DclWrapper {
  // get `ViewContainerRef` using `@ViewChild()` or by injecting it 
  // if the component itself should be the target
  @ViewChild('target', {read: ViewContainerRef}) target;
  @Input() type;
  cmpRef:ComponentRef;
  private isViewInitialized:boolean = false;

  constructor(private dcl:DynamicComponentLoader) {}

  updateComponent() {
    // should be executed every time `type` changes but not before `ngAfterViewInit()` was called 
    // to have `target` initialized
    if(!this.isViewInitialized) {
      return;
    }
    if(this.cmpRef) {
      throw 'currently changing type after the component was added is not supported'
    }
    // LoadIntoLocation was renamed and now takes a `ViewContainerRef`
    // instead of `ElementRef` and `target`
    this.dcl.loadNextToLocation(this.type, this.target).then((cmpRef) => {
      this.cmpRef = cmpRef;
    });
  }

  ngOnChanges() {
    this.updateComponent();
  }

  ngAfterViewInit() {
    this.isViewInitialized = true;
    this.updateComponent();  
  }
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567