0

I'm trying to pass a map from an angular html template to a custom directive.

I guess I succeed to pass it as an object, because I can console.log the map values. But when I try to set or get values from the map I get an error.

My code:

html template

<canvas id="seatsCanvasMain" 
        name="seatsCanvasMain" 
        appTheaterMap [takenSeatsMap]="show.theaterMap" 
        style="border:1px solid black">
</canvas>

Directive:

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({
  selector: '[appTheaterMap]'
})
export class TheaterMapDirective {
  canvas;
  @Input() takenSeatsMap: any;


  constructor(private el: ElementRef) {
    this.canvas = this.el.nativeElement;
  }

  ngOnInit() {

    this.canvas.onclick = (e) => {
      console.log(this.takenSeatsMap);//this will console log the map as an object. at first it seems that it passed correctly
      this.takenSeatsMap.get('4-4'); // ERROR TypeError: _this.takenSeatsMap.get is not a function

    }

  }

}

Here is the console.log enter image description here

Anybody knows what can I do? I would also appreciate other methods on how to pass this map to the directive

Many thanks

codingnighter2000
  • 497
  • 1
  • 7
  • 24

1 Answers1

0

There is no native map to the Object object.

Object.keys(this.map)

will return an array of keys.

try to access as this.takenSeatsMap.4-4

Akj
  • 7,038
  • 3
  • 28
  • 40
  • Why would you initialize an input? Use types... `@Input() takenSeatsMap: Map` – Roberto Zvjerković Jul 17 '18 at 13:33
  • see whether @Input() takenSeatsMap: Map works it will give you 'Generic type 'Map' requires 2 type argument(s)' – Akj Jul 17 '18 at 13:34
  • this.takenSeatsMap.4-4 will result in error as this will be accessing number property on an object, instead if object key is '4-4' (should be string otherwise it will be evaluated as 0), so try accessing it as this.takenSeatsMap['4-4'], this will result in desired result. – Abhishek Kumar Jul 18 '18 at 06:00