Omid Sotooni
Omid Sotooni

Reputation: 199

Get data between two controller that there are no any relation them in angular.js

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"); });
        }
}

Upvotes: 3

Views: 42

Answers (1)

Omid Sotooni
Omid Sotooni

Reputation: 199

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"); });
        }
}

Upvotes: 1

Related Questions