Yelnar
Yelnar

Reputation: 674

Angular how to get access to $scope var from modal

I have a page where I have a button to launch a modal. Both pages has its own controllers. The question is how to get variable from page in modal controller?

Upvotes: 0

Views: 945

Answers (1)

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

You pass data to your modal controller using resolve.

var modalInstance = $modal.open({
    templateUrl: 'template.html',
    controller: 'MyModalCtrl',
    resolve: {
        variableToPass: function () {
            return $scope.items;
        }
    }
});

Then you define your modal controller like this

myApp.controller('MyModalCtrl', ['$scope', $modalInstance'', 'variableToPass', function($scope, $modalInstance, variableToPass) {
    ...
}]);

Alternatively, or complementary, you can pass the whole $scope like this

var modalInstance = $modal.open({
    templateUrl: 'template.html',
    controller: 'MyModalCtrl',
    scope: $scope
});

Upvotes: 1

Related Questions