My journey to learn AngularJS is a continuous process, and I am eager to understand the reasons behind choosing one approach over another in specific scenarios. Is it a matter of personal preference or more than that? Let's explore some examples.
In both scenarios described below, when a user clicks the OK button, the create()
function from the parent controller is triggered by the 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;
},
// Additional resolves if needed
}
});
}
...
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 reason for my inquiry stems from the debate around accessing or injecting objects from a parent/root/container service/controller into another controller/service being considered a "bad practice" in some languages/frameworks I have encountered before.