3

I have two controller that they aren't any relation and want to know smtg in controller1 when happen a change in controller2. like this: Update smtg in controller2 after changing controller1 to understand that.

export class Test1Controller{
        public scope: IScoring;
        $onInit() {
        }
        constructor($scope: IScoring, public $crypto, public toastr: angular.toastr.IToastrService) {          
            this.scope = $scope;
            this.scope.Ctrl = this;
        }
        
        private Understand ()
        {
            // know aboat Test2Controller happens ...
        }
}

export class Test2Controller{
        public scope: IScoring;
        $onInit() {
        }
        constructor($scope: IScoring, public $crypto, public toastr: angular.toastr.IToastrService) {          
            this.scope = $scope;
        }
        
        private Update()
        {
            this.$http.post('https://api.test.com', this.scope.docs).then((response: any) => {
                this.toastr.success("done!");
                this.scope.docs.Document = "";
                },
                function (error) { console.log(error); this.toastr.error("error"); });
        }
}
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Omid Sotooni
  • 159
  • 5
  • 1
    If they are siblings, then would be better to move logic to the parent controller. If not, then you have several options, like: common service, $rootScope.broadcast, $scope.emit & $scope.broadcast – Ihor Yanovchyk Sep 04 '22 at 08:09
  • Thanks @ihor-yanovchyk , i solved it and write it in answer: [Here](https://stackoverflow.com/a/73617039/7617423) – Omid Sotooni Sep 06 '22 at 05:25

1 Answers1

1

I solved it with $scope.$emit in comments ihor-yanovchyk say some way their was usefully.

export class Test1Controller{
        public scope: IScoring;
        $onInit() {
        }
        constructor($scope: IScoring, public $rootScope: any, public $crypto, public toastr: angular.toastr.IToastrService) {          
            this.scope = $scope;
            this.scope.Ctrl = this;
            // understanding about TestController happens ...
            $rootScope.$on('mySearchResultsDone', function(event, data) {
              vm.results = data;
            });
        }
        
        private Understand ()
        {
            // know about Test2Controller happens ...
        }
}

export class Test2Controller{
        public scope: IScoring;
        $onInit() {
        }
        constructor($scope: IScoring, public $rootScope: any, public $crypto, public toastr: angular.toastr.IToastrService) {          
            this.scope = $scope;
        }
        
        private Update()
        {
            this.$http.post('https://api.test.com', this.scope.docs).then((response: any) => {
                this.toastr.success("done!");
                this.scope.docs.Document = "";
                $rootScope.$emit('mySearchResultsDone', {
                   someData: 'myData'
                 });
                },
                function (error) { console.log(error); this.toastr.error("error"); });
        }
}

Omid Sotooni
  • 159
  • 5