Search code examples
javascriptangularjsangularjs-directiveangular-uiui.bootstrap

How to implement multiple templates in one model using angularjs?


This is my code

var app = angular.module('drinks-app', ['ui.bootstrap']);

app.controller('MainCtrl', function($scope, Drink)  {
  'use strict';

  Drink().then(function(drinks) {
    $scope.drinks = drinks;
  });

  $scope.deleteItem = function(item) {
    console.log("deleting item " + item.name);
  };
});


app.factory('Drink', function($http) {
  'use strict';
  return function() {
    return $http.get('drinks.json').then(function(response, status) {
      return response.data;
    });
  };
});

app.directive('ngConfirm', function($modal, $parse) {
  return {
    // So the link function is run before ngClick's, which has priority 0
    priority: -1,

    link: function(scope, element, attrs) {
      element.on('click', function(e) {
        // Don't run ngClick's handler
        e.stopImmediatePropagation();
  
        $modal.open({
          templateUrl: 'ng-delete-template',
          controller: 'ngConfirmController',
          resolve: {
            message: function() {
              return attrs.ngConfirm;
            }
          }
        }).result.then(function() {
          // Pass original click as '$event', just like ngClick
          $parse(attrs.ngClick)(scope, {$event: e});
        });
      });
    }
  };
});

app.controller('ngConfirmController', function($scope, $modalInstance, message) {
  $scope.message = message;
  
  $scope.yes = function() {
    $modalInstance.close();
  };
  
  $scope.no = function() {
    $modalInstance.dismiss();
  };
});
<!DOCTYPE html>
<html ng-app="drinks-app">

  <head>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="style.css">
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.12.0/ui-bootstrap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.12.0/ui-bootstrap-tpls.min.js"></script>
    <script src="script.js"></script>
    <script type="text/ng-template" id="ng-delete-template">
      <div class="modal-body">
        <p>{{message}}</p>
      </div>
      <div class="modal-footer">
        <button class="btn btn-link pull-left" ng-click="no()">No</button>
        <button class="btn btn-primary pull-right" ng-click="yes()">Yes</button>
      </div>
    </script>
    <script type="text/ng-template" id="ng-add-template">
      <div class="modal-body">
      <select >
      <option value="emp" >emplopyee</option>
      </select>
            </div>
      <div class="modal-footer">
        <button class="btn btn-link pull-left">ADD</button>
        
      </div>
    </script>
  </head>

  <body ng-controller="MainCtrl">
    <div class="container">
 
      
      <button class="btn btn-small" type="button" ng-click="deleteItem(drink)" ng-confirm="Are you sure you want to delete '{{drink.name}}'">Delete</button>
      <button class="btn btn-small" type="button" ng-click="deleteItem(drink)" ng-confirm="Are you sure you want to delete '{{drink.name}}'">Add</button>
    </div>
  </body>
</html>

Thsi is my plunker : http://plnkr.co/edit/Gm9lFsGb099w6kCMQoVY?p=preview

In the above code use ui.bootstrap for model popup (delete), now i want to use same model popup for display another template . that means i have two dropdown display list of employees and departments. In previous popup display delete information text only and templateUrl :- assign delete.html page statically. now i want to assign dynamic path to templateUrl in AngularJs model popup


Solution

  • You could pass template name from directive attribute & then place it over $modal.open's templateUrl option like

    <button template-path="abc.html" class="btn btn-small" type="button" 
      ng-click="deleteItem(drink)" ng-confirm="Are you sure you want to delete '{{drink.name}}'">
      Delete
    </button>
    

    Then in directive

    templateUrl: attrs.templatePath || 'ng-confirm-template',
    

    Demo Here