1

I have a list of customers each customer have button more info. I want , when i click on it then showing bootstrap modal by AngularJs controller and then request data by $http.post and getting some more info about this customer and showing info inside modal. How can i do this purpose ? this button :

<button type='button' class='btn btn-primary btn-sm' 
data-ng-click='moreinfo(customer.id)' >more info</button>
2
  • 1
    Add a watch or use broadcast and listen to it in directive Commented Nov 29, 2016 at 8:04
  • can you tell me by example ? Commented Nov 29, 2016 at 8:22

4 Answers 4

2

You can first pass each customer info variable to each more info.

Button like this :

<button type='button' class='btn btn-primary btn-sm btnmargin' 
data-toggle='modal' data-target='#cInfo' data-ng-click='moreinfo(customer)'
 >more info</button>

then you should write this code inside controller :

$scope.customerinfo=[];
$scope.moreinfo= function(customer){
          $scope.customerinfo= customer;
};

Html bootstrap modal :

 <!-- Modal start -->
    <div class='modal fade' id='cinfo' tabindex='-1' role='dialog' 
aria-labelledby='myModalLabel' aria-hidden='true'>
        <div class='modal-dialog modal-lg' role='document'>
            <div class='modal-content'>
                <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal'>
                       <span aria-hidden='true'>&times;</span>
                       <span class='sr-only'>Close</span></button>
                        <h4 class='modal-title text-danger' 
                         id='myModalLabel'>customer info</h4>
                </div>
                <div class='modal-body'>
                     {{customerinfo.firstName}}
                </div>
            <div class='modal-footer'>
               <button type='button' class='btn btn-default' 
            data-dismiss='modal'>close</button>
            </div>
        </div>
    </div>
  </div>
  <!-- Modal end -->

Now you can click on each row button more info and see info in inside modal body.

Sign up to request clarification or add additional context in comments.

Comments

1

Use ngDialog instead of bootstrap modal.

It is easy to implement in angularjs and you can have different controller for it as well and you can definitely transfer data from main page to this ngDialog. https://github.com/likeastore/ngDialog

1 Comment

I don't want use more js plugin because it impact on seo when js file heavy . then i should use bootstrap modal .
1

I will suggest you to go with ui-bootstrap but looking at other answers and considering you do not want to add any more JS library/plugin Hope this helps you

Add a directive called bootstrap-modal as following

app.directive('bootstrapModal', ['$rootScope', '$http', function ($rootScope, $http) {
    "use strict";
    return {
        restrict: "A",
        //add isolated scope if you want
        //scope: {
        //},
        link: function (scope, element) {
            scope.$on('showModal', function (event, object) {
                //fire your ajax here
                $http.get('url').then(function(response){
                    //process your response alter DOM and show modal
                    element.modal('toggle');
                });
            });
        }
    };
}]);

and in your moreInfo function in controller

$scope.moreInfo = function(){
     $rootScope.$broadCast('showModal', dataToPassToListener)
}

You should use the directive with the div which you want to show as modal. As in the same div where you would have given role="dialog" if you would have used simple bootstrap.js

2 Comments

can i write orginal bootstrap modal tag and then i change content of modal body when click each customer more info?
Yes but you have to broadcast the event. And I assume you have only one modal div. If you are planning to have multiple modal divs then you have to use isolated scope.
0

I know that you don't want more JS plugin but I suggest you to use the UI Bootstrap for Angularjs: https://angular-ui.github.io/bootstrap/

It's basically a set of pre-defined directives you can use to load Bootstrap component.

In your case, the thing can end like that:

<button type="button" class="btn btn-primary" ng-click = "moreinfo(customer.id)"> More Info </button>

In your controller :

angular.module('myApp').controller('CustomerInfoCtrl',['$uibModalInstance','$scope', function($uibModalInstance,$scope){

$scope.moreinfo = function(id){
var InfoModal = $uibModalInstance.open({
templateUrl : 'route/to/my/template.html,
controller: 'MoreInfoCtrl',
scope: $scope,
resolve: {
customerId : function(){
    return id;
}
}
});

InfoModal.result.then(function(){
//callback when modal closed
},function(){
//callback when clicked on cancel to dismiss the modal
});

}]);

Then you create another controller, MoreInfoCtrl:

angular.module('myApp').controller('MoreInfoCtrl',['$http','$scope','id', function($http, $scope, id){

//Do your http call with the variable id (i.e the customer.id )

}]);

You have plenty of options. You can easily pass variables, scope or do callback process. I'm using it a lot in a project and it really helps a lot. I suggest you to try it. And it's not really heavy (from above link):

Whichever method you choose the good news that the overall size of a download is fairly small: 122K minified for all directives with templates and 98K without (~31kB with gzip compression, with templates, and 28K gzipped without)

1 Comment

You can also perfectly take the piece of code you are interested to and adapt it to your need. The project provide individual directories for each component in Github.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.