Angular JS - Recipes

Hello World!

Debug Scope: visible = {{visible}}

by Frederik Dietz version 1.0 Recipes with Angular.js Practical concepts and techniques for rapid web applicat...
  • Tên Ebook: Angular JS - Recipes
  • Loại file: PDF
  • Dung lượng: 720 KB
  • Số trang:

LINH TẢI:


TRÍCH DẪN:
Recipes with Angular.js Practical concepts and techniques for rapid web application development

Hello World!

Debug Scope: visible = {{visible}}

by Frederik Dietz version 1.0 Recipes with Angular.js Practical concepts and techniques for rapid web application development Frederik Dietz This book is for sale at http://leanpub.com/recipes-with-angular-js This version was published on 2014-01-12 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2013 - 2014 Frederik Dietz Tweet This Book! Please help Frederik Dietz by spreading the word about this book on Twitter! The suggested hashtag for this book is #recipeswithangularjs. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#recipeswithangularjs Contents An Introduction to Angular.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Including the Angular.js Library Code in an HTML Page . . . . . . . . . . . . . . . . . . . 1 Binding a Text Input to an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Responding to Click Events using Controllers . . . . . . . . . . . . . . . . . . . . . . . . . 3 Converting Expression Output with Filters . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Creating Custom HTML Elements with Directives . . . . . . . . . . . . . . . . . . . . . . 5 An Introduction to Angular.js Including the Angular.js Library Code in an HTML Page Problem You wish to use Angular.js on a web page. Solution In order to get your first Angular.js app up and running you need to include the Angular Javascript file via script tag and make use of the ng-app directive. 1 2 3 6 7 8

This is your first angular expression: {{ 1 + 2 }}

9 10 Tip: You can check out a complete example on github1. Discussion Adding the ng-app directive tells Angular to kick in its magic. The expression {{ 1 + 2 }} is evaluated by Angular and the result 3 is rendered. Note that removing ng-app will result in the browser rendering the expression as is instead of evaluating it. Play around with the expression! You can, for instance, concatenate Strings and invert or combine Boolean values. For reasons of brevity we will skip the boilerplate code in the following recipes. 1 http://github.com/fdietz/recipes-with-angular-js-examples/tree/master/chapter1/recipe1 An Introduction to Angular.js 2 Binding a Text Input to an Expression Problem You want user input to be used in another part of your HTML page. Solution Use Angulars ng-model directive to bind the text input to the expression. 1 Enter your name: 2

Hello {{name}}!

You can find the complete example on github2. Discussion Assigning "name" to the ng-model attribute and using the name variable in an expression will keep both in sync automatically. Typing in the text input will automatically reflect these changes in the paragraph element below. Consider how you would implement this traditionally using jQuery: 1 2 3 4 5 6 Enter your name: 7

8 9 16 17 18 2 https://github.com/fdietz/recipes-with-angular-js-examples/tree/master/chapter1/recipe2 An Introduction to Angular.js 3 On document ready we bind to the keypress event in the text input and replace the text in the paragraph in the callback function. Using jQuery you need to deal with document ready callbacks, element selection, event binding and the context of this. Quite a lot of concepts to swallow and lines of code to maintain! Responding to Click Events using Controllers Problem You wish to hide an HTML element on button click. Solution Use the ng-hide directive in conjunction with a controller to change the visibility status on button click. 1 2 3 4 5 6 7 8
9 10

Hello World!

11
12 13 And the controller in js/app.js: 1 function MyCtrl($scope) { 2 $scope.visible = true; 3 4 $scope.toggle = function() { 5 $scope.visible = !$scope.visible; 6 }; 7 } You can find the complete example on github3. When toggling the button the "Hello World" paragraph will change its visibility. 3 https://github.com/fdietz/recipes-with-angular-js-examples/tree/master/chapter1/recipe3 An Introduction to Angular.js 4 Discussion Using the ng-controller directive, we bind the div element including its children to the context of the MyCtrl controller. The ng-click directive will call the toggle() function of our controller on button click. Note that the ng-show directive is bound to the visible scope variable and will toggle the paragraph's visibility accordingly. The controller implementation defaults the visible attribute to true and toggles its Boolean state in the toggle function. Both the visible variable and the toggle function are defined on the $scope service which is passed to all controller functions automatically via dependency injection. The next chapter will go into all the details of controllers in Angular. For now let us quickly discuss theMVVM(Model-View-ViewModel)patternasused byAngular.IntheMVVMpatternthe modelis plain Javascript,the viewisthe HTMLtemplateandthe ViewModel is the gluebetween the template and the model. The ViewModel makes Angular's two-way binding possible where changes in the model or the template are in sync automatically. In our example, the visible attribute is the model, but it could of course be much more complex , when for example retrieving data from a backend service. The controller is used to define the scope whichrepresents the ViewModel. It interacts with the HTML template by binding the scope variable visible and the function toggle() to it. Converting Expression Output with Filters Problem When presentingdata tothe user, youmightneedto convertthe data to a more user-friendlyformat. In our case we want to uppercase the name value from the previous recipe in the expression. Solution Use the uppercase Angular filter. 1 Enter your name: 2

Hello {{name | uppercase }}!

You can find the complete example on github4. Discussion Angularusesthe |(pipe)characterto combinefilters withvariables inexpressions.When evaluating the expression, the name variable is passed to the uppercase filter. This is similar to working with the Unix bash pipe symbol where an input can be transformed by another program. Also try the lowercase filter! 4 https://github.com/fdietz/recipes-with-angular-js-examples/tree/master/chapter1/recipe4 An Introduction to Angular.js 5 Creating Custom HTML Elements with Directives Problem You wish to render an HTML snippet but hide it conditionally. Solution Create a custom directive which renders your Hello World snippet. 1 2 5
6

Hello World

7
8
Share: