0

Learning AngularJS is a Work In Progress for me so I just want to understand why/when we should use one over another in particular case below. Is it just matter of taste or more than that? See examples below.

In both cases, when user clicks OK button, create() function of parent controller is called from child controller.

RESOLVE STYLE

CreateController

...

var vm = this;

vm.create = create;

function create() {
    console.log('Created!');
}

vm.openCreateModal = function() {
    vm.modalInstance = $uibModal.open({
        ...
        resolve: {
            create: function() {
                return create;
            },
            // Others if any
        }
    });
}

...

CreateModalController

...

vm.ok = function() {
    create();
    $uibModalInstance.close('ok');
};

...

SCOPE STYLE

CreateController

...

var vm = this;

vm.create = create;

function create() {
    console.log('Created!');
}

vm.openCreateModal = function() {
    vm.modalInstance = $uibModal.open({
        ...
        scope: $scope,
        resolve: {
        }
    });
}

...

CreateModalController

...

vm.ok = function() {
    $scope.$parent.vm.create();
    $uibModalInstance.close('ok');
};

...

Update

The actual reason why I ask this question is, accessining/injecting parent/root/container like objects of one service/controller in/to another controller/service is considered as a "bad practise" in some languages/frameworks I use.

2
  • 1
    Usage of $parent is a code smell, a symptom of a deeper problem. The resolve property is more versatile and robust because it can be different for each instance of the modal. It can also be resolved with a promise. Commented Aug 10, 2017 at 1:01
  • @georgeawg I added "Update" section to explain why I came up with this question. You stepped right on it! I just didn't like $scope.$parent... bit for some reason. I just need to wait and see what others say about it. Commented Aug 10, 2017 at 8:26

1 Answer 1

1

The idea of resolve is that it will run that first before initializing the rest of your code. Generally you would use resolve in your routing like so:

$routeProvider
        .when('/', {
            templateUrl: "views/view.html",
            caseInsensitiveMatch: true,
            resolve: {
                load: function() {
                    localStorage['Location'] = "/View";
                }
            }
        })

In the above example resolve will fire the load function before my controller is ever initialized. On the other hand scope is used to bind directly to something in a controller or directive. You should use scope when triggering functions and binding to values between controllers and directives.

To add to this based on the comments below, if the resolve fails it will reject the modal and the window will not open.

Sign up to request clarification or add additional context in comments.

5 Comments

The OP is asking about the resolve property in the UI-Bootstrap Modal Directive.
This is the right answer, however it's worth mentioning that if the resolve fails (the promise rejects) the transition will be canceled and the modal will not show. this pattern is very common in angular.
I added that to my answer. @georgeawg I know he was talking about the bootstrap modal. I was just providing an example of how resolve works in Angularjs and when to use it vs scope like the OP asked.
This answer explains what resolve does in route config, but does not answer the question "... why/when we should use one over another [with UI-Bootstrap Modal] ..."
The answer still gives an explanation. resolve should be used to run code before doing a specific action like opening a window or initializing a controller or directive. It is still pertinent to the question being asked. It is just using routing as an example.

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.