How to pass parameters to a modal?

How to pass parameters to a modal?

I want to pass the userName from a list of userNames a logged in user clicks on to twitter bootstrap modal.
I am using grails with angularjs, where data is rendered via angularjs.
My grails view page encouragement.gsp is



My encourage/_encouragement_modal.gsp is

So, how can I pass userName to encouragementModal?


Solution 1:

To pass the parameter you need to use resolve and inject the items in controller

$scope.Edit = function (Id) {
   var modalInstance = ${
      templateUrl: '/app/views/admin/addeditphone.html',
      controller: 'EditCtrl',
      resolve: {
         editId: function () {
           return Id;

Now if you will use like this:

app.controller('EditCtrl', ['$scope', '$location'
       , function ($scope, $location, editId)

in this case editId will be undefined. You need to inject it, like this:

app.controller('EditCtrl', ['$scope', '$location', 'editId'
     , function ($scope, $location, editId)

Now it will work smooth, I face the same problem many time, once injected, everything start working!

Solution 2:

The other one doesn’t work. According to the docs this is the way you should do it.

angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal) {

    var modalInstance = ${
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      resolve: {
        test: function () {
          return 'test variable';

var ModalInstanceCtrl = function ($scope, $modalInstance, test) {

  $scope.test = test;

See plunkr

Solution 3:

Alternatively you can create a new scope and pass through params via the scope option

var scope = $rootScope.$new();
 scope.params = {editId: $scope.editId};
        scope: scope,
        templateUrl: 'template.html',
        controller: 'Controller',

In your modal controller pass in $scope, you then do not need to pass in and itemsProvider or what ever resolve you named it

modalController = function($scope) {

Solution 4:

You can also easily pass parameters to modal controller by added a new property with instance of modal and get it to modal controller.
For example:

Following is my click event on which i want to open modal view.

 $scope.openMyModalView = function() {
            var modalInstance = ${
                    templateUrl: 'app/userDetailView.html',
                    controller: 'UserDetailCtrl as userDetail'
                // add your parameter with modal instance
                modalInstance.userName = 'xyz';

Modal Controller:

angular.module('myApp').controller('UserDetailCtrl', ['$modalInstance',
                function ($modalInstance) {
                    // get your parameter from modal instance
                    var currentUser = $modalInstance.userName;
                    // do your work...

Solution 5:

I tried as below.

I called ng-click to angularjs controller on Encourage button,

               <tr ng-cloak
                  ng-repeat="user in result.users">
                      <a class="btn btn-primary span11" ng-click="setUsername({{user.userName}})" href="#encouragementModal" data-toggle="modal">

I set userName of encouragementModal from angularjs controller.

     * Encouragement controller for AngularJS
     * @param $scope
     * @param $http
     * @param encouragementService
    function EncouragementController($scope, $http, encouragementService) {
       * set invoice number
      $scope.setUsername = function (username) {
            $scope.userName = username;
    EncouragementController.$inject = [ '$scope', '$http', 'encouragementService' ];

I provided a place(userName) to get value from angularjs controller on encouragementModal.

<div id="encouragementModal" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"
    <h3>Confirm encouragement?</h3>
  <div class="modal-body">
      Do you really want to encourage <b>{{userName}}</b>?
  <div class="modal-footer">
    <button class="btn btn-info"
      ng-click="encourage('${createLink(uri: '/encourage/')}',{{userName}})">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Never Mind</button>

It worked and I saluted myself.

Solution 6:

You should really use Angular UI for that needs. Check it out: Angular UI Dialog

In a nutshell, with Angular UI dialog, you can pass variable from a controller to the dialog controller using resolve. Here’s your “from” controller:

var d = $dialog.dialog({
  backdrop: true,
  keyboard: true,
  backdropClick: true,
  templateUrl:  '<url_of_your_template>',
  controller: 'MyDialogCtrl',
  // Interesting stuff here.
  resolve: {
    username: 'foo'

And in your dialog controller:

  .controller('MyDialogCtrl', function ($scope, username) {
  // Here, username is 'foo'
  $scope.username = username;

EDIT: Since the new version of the ui-dialog, the resolve entry becomes:

resolve: { username: function () { return 'foo'; } }