angularjs: broadcast from directive to controller

后端 未结 3 1149
萌比男神i
萌比男神i 2021-01-11 12:04

Im trying to send a message from within a directive to its parent controller (without success)

Here is my HTML

3条回答
  •  清酒与你
    2021-01-11 12:22

    $broadcast, $emit, and $on are deprecated

    Use of the scope/rootScope event bus is deprecated and will make migration to Angular 2+ more difficult.

    To facilitate making the transition to Angular 2+ easier, AngularJS 1.5 introduced components:

    app.component("myElem", {
        bindings: {
          onGo: '&',
        },
        template: `
            
        `,
        controller: function() {
            this.go = (event,data) => {
                this.onGo({$event: event, $data: data});
            };
        }
    });
    

    Usage:

    angular.module('app', [])
    .controller ('Ctrl', function () {
        this.go = (data) => {
          console.log(data);
          this.update = data;
        };
    })
    .component("myElem", {
        bindings: {
          onGo: '&',
        },
        template: `
          

    `, controller: function() { this.nr = 10; this.go = (event,data) => { this.onGo({$event: event, $data: data}); }; } })
    
    
        

    update={{$ctrl.update}}

    For more information, see

    • AngularJS Developer Guide - Component-based application architecture

提交回复
热议问题