27
AngularJS Tutorial Day #2 BY George

AngularJS GÇô Tutorial-#2.Pptx

Embed Size (px)

DESCRIPTION

tr

Citation preview

Page 1: AngularJS GÇô Tutorial-#2.Pptx

AngularJS Tutorial Day #2

BYGeorge

Page 2: AngularJS GÇô Tutorial-#2.Pptx

Agenda• Directive Overview• Core AngularJS directives

–Ng-app–Ng-controller –Ng-model–Ng-init–Interpolation Directive–Ng-bind–Ng-bind-html-unsafe–Ng-show–Ng-hide–Ng-switch–Ng-if–Ng-include–Ng-repeat–Ng-src–Ng-view–Ng-transclude

Page 3: AngularJS GÇô Tutorial-#2.Pptx

AngularJS - Directives

• AngularJS directives are extended HTML attributes with the prefix ng-.

Page 4: AngularJS GÇô Tutorial-#2.Pptx

Ng-app Directive

• The ng-app directive is the entry point of an AngularJS application.

Page 5: AngularJS GÇô Tutorial-#2.Pptx

Ng-controller Directive

• AngularJS controllers control AngularJS applications.

• And, ng-controller directive defines the controller.

Page 6: AngularJS GÇô Tutorial-#2.Pptx

Ng-model Directive• Used to bind the value in the HTML template & controller • This is the core directive that helps two-way binding• Helps to get the user input and make the impact wherever

that model is used

Page 7: AngularJS GÇô Tutorial-#2.Pptx

Ng-init Directive

• The ng-init directive initialize AngularJS application variables.

Page 8: AngularJS GÇô Tutorial-#2.Pptx

Interpolation Directive• The interpolation directive is one of the most fundamental directives in AngujarJS

• Used to inserts the result of an expression into the HTML template

• Syntax :–{{expression}}

• Examples:

Page 9: AngularJS GÇô Tutorial-#2.Pptx

• The ng-bind directive is an alternative to the interpolation directive.

• Use to insert the result of the model value into the HTML views

• Syntax: –ng-bind = “modelName”

• Examples:

ng-bind Directive

Page 10: AngularJS GÇô Tutorial-#2.Pptx

ng-bind-html-unsafe : Escaping HTML From The Model

• When the input model is “HTML” element, the HTML tags are escaped before inserted into the HTML template

Syntax:• ng-bind-html-unsafe = “ModelName”

Examples:

Page 11: AngularJS GÇô Tutorial-#2.Pptx

ng-show - directive • Used to show html element only when the

model condition is true • If the model condition is false, then it won’t

show the HTML element in the View Syntax:

$scope. modelName = true;ng-show = “modelName”

Examples:

Page 12: AngularJS GÇô Tutorial-#2.Pptx

ng-hide Directives

• This is the reverse form of ng-show – when the condition is true, it will hide the html element. Else, show the html element

Page 13: AngularJS GÇô Tutorial-#2.Pptx

ng-switch Directive• The ng-switch directive is used - if you want to add or remove HTML elements from the DOM

based on data in the model.

Syntax:ng-switch – onng-switch-whenng-switch-default

Example:

Page 14: AngularJS GÇô Tutorial-#2.Pptx

ng-if Directive• The ng-if directive can include / remove HTML

elements from the DOM• This is just like the ng-switch directive, but it has a

simpler syntax. Here is an example:

Page 15: AngularJS GÇô Tutorial-#2.Pptx

ng-include Directive• The ng-include directive can be used to include piece of HTML template from other files into

the view‘s.

Here is an example:

Include HTML with some condition:

Page 16: AngularJS GÇô Tutorial-#2.Pptx

ng-repeat Directive

• The ng-repeat directive is used to iterate over a collection of items and generate HTML from it

Examples:Iterate through Array:

Page 17: AngularJS GÇô Tutorial-#2.Pptx

ng-repeat Directive• Iterate through function:

• Iterate through object:

Page 18: AngularJS GÇô Tutorial-#2.Pptx

Special ng-repeat Variables• The ng-repeat directive defines a set of special

variables which you can use when iterating the collection.

These variables are,•$index•$first•$middle•$last

Page 19: AngularJS GÇô Tutorial-#2.Pptx

ng-repeat Directive

• Repeating Multiple Elements

• Ng-repeat Start & End Keyword:

Page 20: AngularJS GÇô Tutorial-#2.Pptx

Ng-src Directive

• Used to bind the image url dynamically

Page 21: AngularJS GÇô Tutorial-#2.Pptx

Any Questions so far ?

Page 22: AngularJS GÇô Tutorial-#2.Pptx

Move on to Examples

Page 23: AngularJS GÇô Tutorial-#2.Pptx

Assignments #1• Create an application with the model name of

“ofsTraining”. And, then define the controller in the name of “angularJSTraining”

• Inject the above controller & module name into the views

• Create a model name called, “traineesList” in the controller – And, then dynamically display the list in the view in the “ul” “li” tag under the attendees list

Page 24: AngularJS GÇô Tutorial-#2.Pptx

Assignments #2

• In continuation with previous example, when the user click on any one of the trainees name, it should show the details of the selected person – the detail must have the following, “firstname”, “lastname”, “age”, “yearOfExperience”, “dateOfBirth”, “userImage”. If we click on other user, the current detail has to hide and start display the new user details

Page 25: AngularJS GÇô Tutorial-#2.Pptx

Assignments #3

• In continuation with previous example, create a detail section in the separate HTML and include that into the main View (use ng-include)

Page 26: AngularJS GÇô Tutorial-#2.Pptx

Assignments #4

• In continuation with previous example, have a text box in the main view and allow to add new user in the list

• Also, allow to remove the users from the list

Page 27: AngularJS GÇô Tutorial-#2.Pptx