Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing data between two controllers in angularjs

Inside homeController file I have function

 $scope.MyFunction = function () {              
                $http({
                    method: 'POST',
                    url: '/SomeUrl/ActionToDo',
                    data: { id: 1001 },
                }).success(function (response) {
                    if (response.Status == 1) {
                        //success  to do                                
                        }
                        $modal.open({
                            controller: 'modalController',
                            templateUrl: '/app/views/modalTemplate.html',
                            resolve: {
                                myData: function () {
                                    return response;
                                }
                            }
                        });
                    } else {
                        alert(error);
                    }
                }).error(function () {
                    alert('Error!');
                });
            }

Where I'm calling modalController to open modal window. Question is how can I pass data to this controller (modalController) from homeController in order to inject currently selected language which is available inside homeController in variable $scope.selLanguage

like image 825
user1765862 Avatar asked Aug 07 '26 13:08

user1765862


1 Answers

You can inject it into controller as resolve dependency:

$modal.open({
    controller: 'modalController',
    templateUrl: '/app/views/modalTemplate.html',
    resolve: {
        myData: function () {
            return response;
        },
        language: $scope.selLanguage
    }
});

so it will be available in controller as service

.controller('modalController', function(myData, language) {
    console.log(language);
})
like image 149
dfsq Avatar answered Aug 09 '26 04:08

dfsq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!