How to pass data between sibling components without using $scope?

后端 未结 3 1757
深忆病人
深忆病人 2020-11-27 11:08

I am making a component that contains 3 child components in this way:




3条回答
  •  [愿得一人]
    2020-11-27 11:41

    Use custom events to achieve this. you can pass message across your application using event dispatchers $emit(name, args); or $broadcast(name, args); And you can listen for this events using method $on(name, listener);

    Hope it helps

    Ref: https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$emit

    Example: you can notify change like below from your header-component

    $rootScope.$emit("menu-changed", "list");
    

    And you can listen for the change in your main-component directive like

    $rootScope.$on("menu-changed", function(evt, arg){
      console.log(arg);
    });
    

提交回复
热议问题