3

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

2 Answers 2

1

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);
})
Sign up to request clarification or add additional context in comments.

Comments

0

passed the data as locals to the modal controller using resolve.

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

In modalController refer that variable as it is local to that modalController controller.

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

  $scope.modalVar= modalVar; 
});

Please refer this for more

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.