0

I'm trying to manipulate data inside a modal in AngularJS and after that, view it outside of the modal. So I have to pass the data from the inner controller (which is used by the modal) to the outer controller. To put the data into the inner controller from the outer one I've done this with help of the option resolve of $modal.open :

myApp.controller('MyCtrl', function ($scope, $modal) {

$scope.number = 1;

$scope.openModal = function () {
    $modal.open({
        templateUrl: 'myModalContent.html',
        backdrop: true,
        windowClass: 'modal',
        controller: function ($scope, $modalInstance, number) {
            $scope.number = number;
            $scope.submit = function () {

                //How to iterate var number in the outer controller?
                //$scope.number++;

                $modalInstance.dismiss('cancel');
            }
        },
        resolve: {
            number: function () {
                return $scope.number;
            }
        }
    });
   };
});

Now I want to count number up in the modal and show it outside of the modal. $scope.number affects obviously just the number in the current controller, so how can I set the variable of the outer controller?

Here's my Fiddle.

Your help would be appreciated.

1
  • I'm no Angular expert, but I think the recommended approach is to use a service. The service has its own scope, and you inject the service into each controller so the controllers have access to the common variable/value. Commented May 28, 2015 at 15:29

3 Answers 3

1

Try _scope = $scope; to hold on reference to outside controller. And inside inner controller, use _scope.number++; Working fiddle:

"http://jsfiddle.net/5xop3wL9/5/"
Sign up to request clarification or add additional context in comments.

Comments

0

You can add a reference to $scope of outer controller and then increment the number using the new reference.

...
$scope.number = 1;
$higherScope = $scope;
...
//Inside submit
$scope.submit = function () {

                    //How to iterate var number in the outer controller?
                    $higherScope.number++;

                    $modalInstance.dismiss('cancel');
                }

Updated Fiddle

2 Comments

Thanks. Is this a better practice than using a service in my case?
No, it's not better practice. Saving data to a service object to be passed around your application is much cleaner than attaching things to scopes.
0

I would save your data to a service so it can be passed between controllers.

myApp.factory('modalDataService', function () {
    var modalData = {}; 
    return {
        getData: function () {
            return modalData;
        },
        setData: function (newModalData) {
            modalData = newModalData;
        },
        resetData: function () {
            modalData = {};
        }
    };
});

You can then access your data from your controllers by passing in the factory as a dependency and calling functions like this:

modalDataService.getData();
modalDataService.setData(newModalData);
modalDataService.resetData();

Comments

Your Answer

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