How can I make data available in a controller? I have created a really simple Plunk that should show data on the $scope in a modal. I’ll then need to update the data, and only update $scope on clicking “ok”. Clicking “cancel” will discard the changes.
But before I get to that step, I need to make the scope available to the modal. Most of the examples use two controllers. Do I need another controller as in this example: Passing Data to Twitter Bootstrap Modal in Angular? In my controller I have the following:
$scope.open = function(){
var modalInstance = $uibModal.open({
templateUrl: 'modal.html',
controller: 'ModalInstanceController',
resolve: {
users: function() {
return $scope.users;
}
}
});
};
How can I display the users in the template? The plunk is here: http://plnkr.co/edit/FuXjSwtljQtFYOtFRV18?p=preview
4
Answers
To be able to access scope of the controller you need to pass scope object to the modal when creating an instance of it:
This way Angular will create child scope of the controller
$scope
so you will be able to access items inside of modals scope:Demo: http://plnkr.co/edit/0m9oktX2JHFmeiaDfOpO?p=preview
you can do it with just one controller, is just that is a “dirty” solution, because both html files will share the same controller, which is potentially an issue.
the problem you are facing is that in the modal you don’t have a defined
scope
, so theforeach (ng-repeat)
you are doing is not getting any elementsyou can fix it easily by changing your modal.html to
as you see, now this modal has a controller (the same as the main window) and so will have a
scope
or else just pass the scope to the modal definition adding
is dirty, and you are “polluting” the scope, but it works 🙂
Here is some code from my current project it works as expected if you want to use any ng-click on the modal your function has to reside in ModalInstanceController
You can access scope in modal –