I want to pass the userName
from a list of userName
s 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
<div ng-controller="EncouragementController"> <g:render template="encourage/encouragement_modal" /> <tr ng-cloak ng-repeat="user in result.users"> <td>{{user.userName}}</rd> <td> <a class="btn btn-primary span11" href="#encouragementModal" data-toggle="modal"> Encourage </a> </td> </tr>
My encourage/_encouragement_modal.gsp
is
<div id="encouragementModal" class="modal hide fade"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h3>Confirm encouragement?</h3> </div> <div class="modal-body"> Do you really want to encourage <b>{{aModel.userName}}</b>? </div> <div class="modal-footer"> <button class="btn btn-info" ng-click="encourage('${createLink(uri: '/encourage/')}',{{aModel.userName}})"> Confirm </button> <button class="btn" data-dismiss="modal" aria-hidden="true">Never Mind</button> </div> </div>
So, how can I pass userName
to encouragementModal
?
Data can be passed to the modal body from the HTML document which gets displayed when the modal pops up. To pass data into the modal body jquery methods are used. jQuery is similar to JavaScript, however jQuery methods are simple and easier to implement. jQuery reduces the lines of code.
To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.
To pass the parameter you need to use resolve and inject the items in controller
$scope.Edit = function (Id) { var modalInstance = $modal.open({ 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!
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 = $modal.open({ templateUrl: 'myModalContent.html', controller: ModalInstanceCtrl, resolve: { test: function () { return 'test variable'; } } }); }; var ModalInstanceCtrl = function ($scope, $modalInstance, test) { $scope.test = test; };
See plunkr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With