AngularJS & Bootstrap Tabs, Forms, Models Tabs inside out - - PowerPoint PPT Presentation

angularjs bootstrap
SMART_READER_LITE
LIVE PREVIEW

AngularJS & Bootstrap Tabs, Forms, Models Tabs inside out - - PowerPoint PPT Presentation

AngularJS & Bootstrap Tabs, Forms, Models Tabs inside out Bootstrap has classes that make it easy to create tabs But how do we give behavior to these tabs? How tabs work: If you select 1 tab, o it becomes active and o its


slide-1
SLIDE 1

AngularJS & Bootstrap

Tabs, Forms, Models

slide-2
SLIDE 2

Tabs inside out

  • Bootstrap has classes that make it easy to create

tabs

  • But how do we give behavior to these tabs?
  • How tabs work:
  • If you select 1 tab,
  • it becomes active and
  • its content gets displayed on the page
  • How do we select a tab?
  • using ng-click
  • ng-click takes expression that assigns a value to a variable, say tab.
  • tab == 1 when we click on the first, etc.
slide-3
SLIDE 3

Example using tabs

<section ng-init="tab = 1”> <ul class="nav nav-pills"> <li> <a href href ng ng-click click="tab=1" tab=1"> Description > Description </ </a> </li> <li> <a href ng-click="tab=2">Specs </a> </li> <li> <a href ng-click="tab=3">Reviews </a> </li> </ul> {{tab}} <!– debugging, display tab --> </section>

slide-4
SLIDE 4

2-way Data Binding

  • When ng-click="tab = 1" changes the value of tab,

the {{tab}} expression automatically gets updated

  • This means expressions are re-evaluated when a

property changes:

  • E.g., the value of tab changes
  • Expressions that refer to tab get re-evaluated when that happens
  • Change to view updates model AND change to

Model updates view

slide-5
SLIDE 5

Clicking link updates value of tab

<section> <ul class="nav nav-pills"> <li ng-class="{active:tab===1}"> <a href href ng ng-click click="tab=1" tab=1"> Description > Description </ </a> </li> <li ng-class="{active:tab===2}"> <a href href ng ng-click click="tab=2 tab=2">Specs Specs </ </a> </li> <li ng-class="{active:tab===3}"> <a href href ng ng-click click="tab tab=3 =3">Reviews Reviews </ </a> </li> </ul> </section>

slide-6
SLIDE 6

Changing tab triggers which panel to show

<div class="panel" ng-show="tab === 1"> <h4>Description </h4> <p>{{product.description}}</p> </div> <div class="panel" ng-show="tab === 2"> <h4>Specifications </h4> <blockquote>None yet</blockquote> </div> <div class="panel" ng-show="tab === 3"> <h4>Reviews</h4> <blockquote>None yet</blockquote> </div>

slide-7
SLIDE 7

Setting initial values

  • Use ng-init to set initial value of an expression

<section section ng ng-init init="ta tab = = 1" 1">

slide-8
SLIDE 8

Changing tab updates <li> element

<section ng-init="tab = 1"> <ul class="nav nav-pills"> <li ng-class="{active:tab===1}"> <a href href ng ng-click click="tab=1" tab=1"> Description > Description </ </a> </li> <li ng-class="{active:tab===2}"> <a href href ng ng-click click="tab=2 tab=2">Specs Specs </ </a> </li> <li ng-class="{active:tab===3}"> <a href href ng ng-click click="tab tab=3 =3">Reviews Reviews </ </a> </li> </ul> </section>

slide-9
SLIDE 9

Cleaning dirty code

  • Application's logic is inside our HTML.
  • Does not feel right
  • Makes code dirty
  • Pull logic out of HTML to organize it
  • Maybe in a controller
  • Makes code look clean
  • Initialization, assignment, comparison code belong in controller
slide-10
SLIDE 10

Forms and models

  • Write code for basic html form
  • Give form a name, will be useful
  • Add code for live preview of form data
  • Bind an object (model) to the form element using

ng-model directive

  • Use bootstrap css classes to embellish form
  • Remember to submit the form
slide-11
SLIDE 11

Binding Elements

  • With Checkbox: bind the ng-model to the

checkbox. <input ng-model="review.terms" type="checkbox" /> I agree to the terms.

  • This sets value to true or false.
slide-12
SLIDE 12

Binding Elements

  • With Radio Buttons:

<input ng-model="review.color" type="radio" value="red" /> Red <input ng-model="review.color" type="radio" value="blue" /> Blue <input ng-model="review.color" type="radio" value="green" /> Green

  • Sets the proper value based on which is selected.
slide-13
SLIDE 13

Submitting form

  • Use ng-submit directive
  • allows us to call a function when the form is submitted.
  • Define submission function in controller
  • Remember to initialize object that form will use
  • Can be an empty object
  • Reset form to remove previous values
  • Next time è Form validation
slide-14
SLIDE 14

Resources

  • https://angularjs.org
  • http://getbootstrap.com/css/#forms
  • https://www.codeschool.com/paths/javascript