# Angular UI Bootstrap Learn how to use the Bootstrap JS plugins within an AngularJS app. This material is part of [web development courses](https://github.com/MediaComem/comem-webdev) for [Media Engineering](https://heig-vd.ch/formations/bachelor/filieres/ingenierie-des-medias). **You will need** * [Google Chrome][chrome] (recommended, any browser with developer tools will do) * [Sublime Text][sublime] (recommended, any code editor will do... **except Notepad**) **Recommended reading** * [Bootstrap][bootstrap] * [Javascript][js] * [Angular][ng] --- ## Bootstrap JS .breadcrumbs[
Angular UI Bootstrap
] Bootstrap is **mainly** an HTML/CSS framework, as seen in the dedicated course. But Bootstrap developpers decided to **enhance the Bootstrap features** with some JS code, more precisely code using jQuery. This JS code allows more interesting and advanced behaviors. > Bootstrap JS could be seen as a library. --- ### Include Bootstrap JS .breadcrumbs[
Angular UI Bootstrap
>
Bootstrap JS
] In the same fashion as the Bootstrap CSS file, you can include the Bootstrap JS file in two ways: > We won't do a live example with this Bootstrap JS file. * Referencing a CDN url in your `index.html`: ```html ``` * Downloading Bootstrap, moving the `bootstrap.min.js` file in the `js` directory of your project directory, then referencing it in your `index.html`: ```html
``` > Be extra sure to add this ` ``` Finally, don't forget to **add the dependency** in your app's `module`: ```js angular.module('myModule', ['`ui.bootstrap`']); ``` --- class: center, middle ## Documentation .breadcrumbs[
Angular UI Bootstrap
] As always, the prime source of information about the library is it's official **documentation**. In the case of Angular UI Bootstrap, you'll find it [here][uib-doc] > Angular UI Bootstrap contains **all Bootstrap JS components**, along with some additionnal behaviors --- ## Dropdown Menus (Again) .breadcrumbs[
Angular UI Bootstrap
] Using Bootstrap Dropdown with Angular UI Bootstrap is very **simple**. You'll need to respect the **same HTML Bootstrap Markup** as before, but will need to **remove** all the `data-*` attribute, and replace them with **directives**: ```html
Dropdown trigger
Element 1
Element 2
``` --- ## Modals (Again) .breadcrumbs[
Angular UI Bootstrap
] On the other hand, modals are a **little bit** more complicated to implement, because you'll need to **add some code**. This time, you can export the modal markup in its own template file: ```html
×
Modal title
Here goes the content.
``` > The `div.modal.fade` and `div.modal-dialog` elements have been deleted. > The `data-dismiss` attribute have been replaced by `ng-click` directives. --- ### The Modal Trigger .breadcrumbs[
Angular UI Bootstrap
>
Modals (Again)
] The button that previously triggered the showing of the modal needs to be **changed**. It will no longer rely on `data-*` attribute, but will instead trigger **a call to a function in its controller**. You can remove all `data-*` attributes from the button, and **replace** them with a `ng-click` directive, with the name of **the function to trigger**: ```html
Log in
``` --- ### The Trigger Function .breadcrumbs[
Angular UI Bootstrap
>
Modals (Again)
] Now, in the controller that's **responsible for this button**, add a new service dependency, called `$uibModal`. Use this service's `.open()` method to create the function that will **show the modal** when triggered by the button. ```js angular.module("myApp").controller("myController", function(`$uibModal`) { var myCtrl = this; myCtrl.openModal = function() { `$uibModal.open`({ // Pass the path to the file containing your modal template templateUrl: "modal/template.html", // You can assign your modal its own controller controller: "myModalController", // The alias for the modal's controller controllerAs: "modal" }); }; }); ``` --- ### The Modal Controller .breadcrumbs[
Angular UI Bootstrap
>
Modals (Again)
] As said in the previous slide, you can assign **its own controller** to your modal. In this controller, if you want to **access the current modal instance**, you'll need to inject **a new service dependency**, called `$uibModalInstance`. With this service, you could for example create a function that will **close the currently opened modal**, using the `.close()` method: ```js angular.module("myApp") .controller("myModalController", function($`uibModalInstance`) { var myModalCtrl = this; // This function could be called by a button in the modal template myModalCtrl.close = `$uibModalInstance.close`; }); ``` --- class: middle ## Resources .breadcrumbs[
Angular UI Bootstrap
] **Documentation** * [Angular UI Bootstrap Documentation][uib-doc] [js]: ../js [bootstrap]: ../bootstrap [ng]: ../angular [git]: ../git/ [chrome]: https://www.google.com/chrome/ [sublime]: https://www.sublimetext.com/ [auib]: https://angular-ui.github.io/bootstrap/ [uib-doc]: https://angular-ui.github.io/bootstrap/