0

its like signup menu first controller gets the data from the user and after the validation it sends the data to $rootScope and broadcast it so the second controller can get it and show the data in myInfo tab

don't know whats wrong but the second controller cant get the data

first controller

(function() {
  "use strict";
  angular.module("public").controller("SignUpController", SignUpController);
      SignUpController.$inject = ["$rootScope"];
      function SignUpController($rootScope) {
        let signUpCtrl = this;
        signUpCtrl.name = "";
        signUpCtrl.email = "";

        signUpCtrl.submit = () => {

          signUpCtrl.formSbmitted = true;

          $rootScope.$broadcast("user:created", {
            name: signUpCtrl.name,
            email: signUpCtrl.email
          });
        };
      }
})();

second controller

(function() {
  "use strict";
  angular.module("public").controller("MyInfoController", MyInfoController);
      MyInfoController.$inject = ["$rootScope"];
      function MyInfoController($rootScope) {
        let myInfo = this;

        $rootScope.$on("user:created", function(event, data) {
          myInfo.data = data;
        });
      }
})();
georgeawg
  • 48,608
  • 13
  • 72
  • 95
sirH
  • 83
  • 5
  • Check out this [link](https://stackoverflow.com/questions/24830679/why-do-we-use-rootscope-broadcast-in-angularjs). Do NOT use $rootScope.$on use $scope.$on! Also you may need to call $rootScope.$broadcast.$apply. – Mickers Dec 06 '19 at 19:37
  • 1
    @Mickers In the context of this question, `$rootScope.$broadcast.$apply` is not needed and will in fact cause an error. Good catch with `$rootScope.$on`. It has potential for memory leaks. – georgeawg Dec 06 '19 at 19:49
  • Works fine in this [DEMO on PLNKR](https://plnkr.co/edit/3PspJpvSygv12f0XJFCJ?p=preview). – georgeawg Dec 06 '19 at 20:22
  • yes it works when both controllers are in same view but when i use ui-router and everyone of then becomes responsible for different view it doesn't work as if the second controller is not instantiated yet – sirH Dec 07 '19 at 22:15

0 Answers0