Sharing data between two controllers in angularjs

Inside my homeController file I have a 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 call modalController to open the modal window. The question is, how can I pass data to this controller (modalController) from homeController to inject the currently selected language which is available inside homeController in a variable$scope.selLanguage

+3


source to share


2 answers


You can inject it into the controller depending on the dependency:

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

      



so it will be available in the controller as a service

.controller('modalController', function(myData, language) {
    console.log(language);
})

      

+1


source


passed data as local users to modal controller using permission.

resolve: {
        myData: function () {
            return response;
        },
        modalVar: $scope.selLanguage
    }

      

The modalController refers to this variable because it is local to this modalController.



angular.module('demo').controller('modalController', function ($scope, $modalInstance, modalVar) {

  $scope.modalVar= modalVar; 
});

      

Please refer this for more

0


source







All Articles