Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between modal and controller

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

like image 532
Geraint Anderson Avatar asked Mar 01 '16 11:03

Geraint Anderson


People also ask

How do you pass data into an ionic modal controller?

Passing data to an Ionic modal This is as simple as calling the componentProps on our modalController create function. number: number = 3; const modal = await this. modalController. create({ component: DetailPage, componentProps: { number: this.

How pass data from modal to controller in laravel?

add a different rout: Route::POST('/dashboard/update/{id}', 'DashboardController@update'); Controller: public function update(Request $request, $id) { $the_id = $id; //this is the id. dd($id); //to check your id value. }

What is data dismiss modal?

modal-header class is used to define the style for the header of the modal. The <button> inside the header has a data-dismiss="modal" attribute which closes the modal if you click on it. The . close class styles the close button, and the . modal-title class styles the header with a proper line-height.


1 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:

  $scope.open = function() {
    var modalinstance = $uibModal.open({
      scope: $scope,
      templateUrl: 'modal.html',
      resolve: {
        users: function() {
          return $scope.users;
        }
      }
    })
  };

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

like image 179
dfsq Avatar answered Oct 15 '22 08:10

dfsq