Monday, May 14, 2018

Open a modal in AngularJS using $uibModal Service

5 comments
 You will often have to launch popup modals on your SPA when certain events are fired . For example when click a button or when you double-click a grid row or when you hit enter using your keyboard on a ui-grid row .For all these scenarios ,$uibModal  service can be used to create popup windows in your Angular SPA.

To achieve this you need to include the bootstrap module as a dependency and add it to your application as shown below 

      angular.module('app',[ 'ui.bootstrap'])


<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">


Once the dependencies are injected , since the modal needs its own html template and controller , go ahead and create a template file(html) and controller for the modal .All the functions associated with the modal need to be defined inside this controller . Inject the $uibModal service into the main controller so that you can call its open() method which launches our popup.

 controller('mainController', function mainController( $scope,$uibModal )
     ...
        $scope.openModal  = function(){
         $uibModal.open({
               templateUrl : 'modalTemplate.html',
               controller  : 'modalController'
         });
              ....

This open method returns a modal instance ,with properties which can be referenced to perform other actions like close on the modal . Please refer the documentation in doc for more details on $uibModal and its methods. 

The controller for the modal instance has a special  $uibModalInstance injectable to access the modal instance  and perform actions like close. In our example I have implemented the cancel button and ok button to close the modal when clicked.

   ...
     controller('modalController', function modalController( $scope , $uibModalInstance )
      
        $scope.cancel = function(){
     
           $uibModalInstance.close()
       }
         ......


Here is the demo:




You can also create your own service to launch the modal and make it reusable i.e call the service from any of your angular pages and you can launch the modal window . Below is an example for launching a modal using a service .




5 comments :

  1. Replies
    1. Open A Modal In Angularjs Using $Uibmodal Service >>>>> Download Now

      >>>>> Download Full

      Open A Modal In Angularjs Using $Uibmodal Service >>>>> Download LINK

      >>>>> Download Now

      Open A Modal In Angularjs Using $Uibmodal Service >>>>> Download Full

      >>>>> Download LINK 7l

      Delete
  2. Hiii...Thanks for sharing Great info...Nice post...Keep move on...
    Angular JS Training in Hyderabad

    ReplyDelete
  3. Thanks for sharing this blog Very useful Post. Hire AngularJS developer to create enriching content and impress maximum readers and potential customers. and other web and app development services for your business needs

    hire angularjs developer
    hire front end developer
    hire ios developer

    ReplyDelete
  4. Open A Modal In Angularjs Using $Uibmodal Service >>>>> Download Now

    >>>>> Download Full

    Open A Modal In Angularjs Using $Uibmodal Service >>>>> Download LINK

    >>>>> Download Now

    Open A Modal In Angularjs Using $Uibmodal Service >>>>> Download Full

    >>>>> Download LINK DR

    ReplyDelete