Angular2 calling custom function after ngSwitch new view is created

后端 未结 3 1508
粉色の甜心
粉色の甜心 2020-11-30 14:23

I am creating a small app using Angular2+Ionic2. In this app I want initialise google-map into view segment when user

3条回答
  •  既然无缘
    2020-11-30 15:04

    There is no built-in support of calling a function when ngSwitch adds/removes elements.

    I would create a directive that calls the function when created (for example in ngOnInit()) or emits an event where an event handler can be bound to, and apply it to the component that is added when the ngSwitch branch is enabled.

    If ngSwitch adds a component you can implement it in this component as well (depends on your requirements)

    update

        
           Listing
        
    
    @Directive(selector: '[onCreate]')
    export class OnCreate implements OnInit {
      @Output() onCreate:EventEmitter = new EventEmitter();
    
      ngOnInit() {
        this.onCreate.emit('dummy');
      }
    }
    

    Plunker example

提交回复
热议问题