0

I am new to Angular JS. I started learning Angular JS today. Before I was using jQuery and Bootstrap for front-end. Now I want to show Bootstrap dialog box using AngularJS. I found this, https://angular-ui.github.io/bootstrap/.

But there are so many things difficult for beginners to understand. How can I correct my code to show bootstrap dialog?

This is my code

   <html>
<head>
  <title>Angular</title>
  <link rel="stylesheet" href="http://localhost:8888/angular/bootstrap.css"></link>
  <script src="http://localhost:8888/angular/angular-js.min.js"></script>
  <script src="http://localhost:8888/angular/angular-js.animate.min.js"></script>
  <script src="http://localhost:8888/angular/angular-js.sanitize.min.js"></script>
  <script src="http://localhost:8888/angular/ui-bootstrap-tpls.min.js"></script>

</head>
<body>
  <div ng-controller="ModalDemoCtrl as $ctrl">
      <script type="text/ng-template" id="myModalContent.html">
          <div class="modal-header">
              <h3 class="modal-title" id="modal-title">I'm a modal!</h3>
          </div>
          <div class="modal-body" id="modal-body">
              <ul>
                  <li ng-repeat="item in $ctrl.items">
                      <a href="#" ng-click="$event.preventDefault(); $ctrl.selected.item = item">{{ item }}</a>
                  </li>
              </ul>
              Selected: <b>{{ $ctrl.selected.item }}</b>
          </div>
          <div class="modal-footer">
              <button class="btn btn-primary" type="button" ng-click="$ctrl.ok()">OK</button>
              <button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">Cancel</button>
          </div>
      </script>

      <button type="button" class="btn btn-default" ng-click="$ctrl.open()">Open me!</button>
      <button type="button" class="btn btn-default" ng-click="$ctrl.open('lg')">Large modal</button>
      <button type="button" class="btn btn-default" ng-click="$ctrl.open('sm')">Small modal</button>
      <button type="button" class="btn btn-default" ng-click="$ctrl.toggleAnimation()">Toggle Animation ({{ $ctrl.animationsEnabled }})</button>
      <button type="button" class="btn btn-default" ng-click="$ctrl.openComponentModal()">Open a component modal!</button>
      <div ng-show="$ctrl.selected">Selection from a modal: {{ $ctrl.selected }}</div>
  </div>
</body>
<script>
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($uibModal, $log) {
  var $ctrl = this;
  $ctrl.items = ['item1', 'item2', 'item3'];

  $ctrl.animationsEnabled = true;

  $ctrl.open = function (size) {
    var modalInstance = $uibModal.open({
      animation: $ctrl.animationsEnabled,
      ariaLabelledBy: 'modal-title',
      ariaDescribedBy: 'modal-body',
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      controllerAs: '$ctrl',
      size: size,
      resolve: {
        items: function () {
          return $ctrl.items;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      $ctrl.selected = selectedItem;
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };

  $ctrl.openComponentModal = function () {
    var modalInstance = $uibModal.open({
      animation: $ctrl.animationsEnabled,
      component: 'modalComponent',
      resolve: {
        items: function () {
          return $ctrl.items;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      $ctrl.selected = selectedItem;
    }, function () {
      $log.info('modal-component dismissed at: ' + new Date());
    });
  };

  $ctrl.toggleAnimation = function () {
    $ctrl.animationsEnabled = !$ctrl.animationsEnabled;
  };
});

// Please note that $uibModalInstance represents a modal window (instance) dependency.
// It is not the same as the $uibModal service used above.

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($uibModalInstance, items) {
  var $ctrl = this;
  $ctrl.items = items;
  $ctrl.selected = {
    item: $ctrl.items[0]
  };

  $ctrl.ok = function () {
    $uibModalInstance.close($ctrl.selected.item);
  };

  $ctrl.cancel = function () {
    $uibModalInstance.dismiss('cancel');
  };
});

// Please note that the close and dismiss bindings are from $uibModalInstance.

angular.module('ui.bootstrap.demo').component('modalComponent', {
  templateUrl: 'myModalContent.html',
  bindings: {
    resolve: '<',
    close: '&',
    dismiss: '&'
  },
  controller: function () {
    var $ctrl = this;

    $ctrl.$onInit = function () {
      $ctrl.items = $ctrl.resolve.items;
      $ctrl.selected = {
        item: $ctrl.items[0]
      };
    };

    $ctrl.ok = function () {
      $ctrl.close({$value: $ctrl.selected.item});
    };

    $ctrl.cancel = function () {
      $ctrl.dismiss({$value: 'cancel'});
    };
  }
});
</script>
</html>

But my code is not working. There are too many options I do not know in this for example "myModalContent.html" is from where? Would you explain step by step to show Bootstrap dialog box in AngularJS?

I tried this way as well

<html>
<head>
  <title>Angular</title>
  <script src="angular-js.min.js"></script>
  <script src="angular-js.animate.min.js"></script>
  <script src="angular-js.sanitize.min.js"></script>
  <script src="angular-js.touch.min.js"></script>
  <script src="bootstrap.ui.js"></script>
  <link href="bootstrap.css" rel="stylesheet">

</head>
<body ng-app="MyApp" ng-controller="MyCtrl">

  <button class="btn" ng-click="open()">Open Modal</button>

  <div modal="showModal" close="cancel()">
    <div class="modal-header">
      <h4>Modal Dialog</h4>
    </div>
    <div class="modal-body">
      <p>Example paragraph with some text.</p>
    </div>
  </div>

</body>
<script>
var app = angular.module("MyApp",["ui.bootstrap.modal"]);

app.controller('MyCtrl',function($scope){
  $scope.open = function() {
    $scope.showModal = true;
  };
})
</script>
</html>

It shows like this:

enter image description here

It is not working as well. How can I show bootstrap dialog in AngularJS?

8
  • you got some syntax errors there. ng-controller="myCtrl as as $ctrl" you got twice the as keyword. Furthermore I think the use of the dollar sign for a controller alias is invalid. Use myCtrl as vm instead. Commented Sep 14, 2016 at 13:59
  • I updated the question. Please have a look. Code is still not working. :( Commented Sep 14, 2016 at 14:17
  • have you checked their working sample on plunker?plnkr.co/edit/VoLvFSg5d70Zq64w6UI3?p=preview Commented Sep 14, 2016 at 14:29
  • I agree with @cleftheris the dollar sign for a controller. Did you update that as well? Commented Sep 14, 2016 at 14:51
  • @WaiYanHein are you still having trouble? Commented Sep 15, 2016 at 13:19

2 Answers 2

1

You don't have a ctrl.open function although you ref one in your ng-click.

Maybe you can try this?

$ctrl.open = function (size) {
                var modalInstance = $uibModal.open({
                    animation: $ctrl.animationsEnabled,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'myModalContent.html',
                    controller: 'ModalInstanceCtrl',
                    controllerAs: '$ctrl',
                    size: size,
                    resolve: {
                        items: function () {
                            return $ctrl.items;
                        }
                    }
                });
Sign up to request clarification or add additional context in comments.

Comments

1

open() method returns a modal instance. This is what actually creates the modal on your template.

templateurl is a path to your template. Here, its looking for myModalContent.html in the same path as your controller code. You need to create and design the template and that will get loaded when this modal is called in your view.

template can be used here instead of templateurl for inline HTML if you do not have enough HTML. "enough" is subjective to your understanding.

controller is the name of your controller. You can define it as you like.

controllerAs is the alias of your controller. You can define it to be what you like too. $ctrl is the alias of your controller by default. If you remove the controllerAs option from here, you can still use $ctrl in your template. Although, using this requires you to have controller option provided.

animationsenabled is a boolean value which can be used to toggle modal animation in your view by toggling the animation property. Default is false.

resolve helps create a $resolve object on the opened modal. This helps provide all resolved values from your items. Its basically a configuration on the $routeprovider service of Angular. To help understand $resolve better, you can have a look at this

Since you are new to Angular, much like me, a great place to start would be to understand Services in Angular. Its going to be one of your most used feature.

Moreover, you can check out plunker of the above code, edit it and understand the functioning. I tried it yesterday for the first time too.

Also, add angular-ui as a tag to your question so the angular-ui guys can get involved.

Hope this helps in some way.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.