SlideShare a Scribd company logo
AngularJS
Style Guide
1
2
Style Guide
Debugging AngularJS
Common Errors
Style Guide
Reference : JohnPapa & mgechev
3
4
Single Responsibility [Style Y001]
Avoid putting app modules, controllers, factories in the same file
Recommended 1 Component Per File
5
IIFE [Style Y010]
Wrap angular module inside immediately invoke function expression (IIFE)
Why? An IIFE removes variables from the global scope. This helps prevent
variables and function declarations from living longer than expected in
the global scope, which also helps avoid variable collisions.
Why?: When your code is minified and bundled into a single file for
deployment to a production server, you could have collisions of variables
and many global variables. An IIFE protects you against both of these by
providing variable scope for each file.
Strict mode : cannot use
undeclared variable
6
Definition of Modules (Setters) [Style Y021]
Avoid using a variable name when declare module
Why? With 1 Component Per File,
there is rarely a need to introduce
a variable for the module
Recommended to use a simpler setter syntax
7
Getters [Style Y022]
Avoid using a variable name when using module
Recommended to use chaining with the getter syntax
Why? This produces more readable code and avoids variable
collisions or leaks
8
controllerAs view syntax [Style Y030]
Avoid using classic controller with $scope syntax
Use the controllerAs syntax
Why?: It promotes the use of binding to a "dotted" object in the View (e.g.
customer.name instead of name), which is more contextual, easier to
read, and avoids any reference issues that may occur without "dotting".
9
controllerAs with “self” [Style Y032]
*** NOTE : In Our Project, use “self” instead of “vm” for consistency
Use a capture variable for “this” when using the controllerAs syntax. Choose
a consistent variable name such as vm, which stands for ViewModel
var self = this;
self.name = {};
self.sendMessage = function() {};
10
Function Declarations to Hide Implementation
Details [Style Y034]
Why?: You never have to worry
with function declarations that
moving var a before var b will
break your code because a
depends on b.
Why?: Move the complexity
out of view so can see
important stuff up top
Why?: Easier to read and
identify which function can
be bound to view
Why?: Order is critical with
function expressions
example of function expression :
var a = function() { };
var b = function() { };
11
Defer Controller Logic to Services [Style Y035]
Why?: Logic may be reused by multiple controllers when placed
within a service and exposed via a function.
Why?: Removes dependencies and hides implementation
details from the controller.
12
Order of Dependency Injection
Put angular built in service at the front and self-defined service at
the back
Why?: Improve readability and consistency
<input type="text" ng-model=“{{name}}” my-directive=“{{data}}” />
Debugging AngularJS
13
or Ionic Project
14
Tips on Debugging AngularJS
> Use scope inspection tools
Always press command+option+I (Mac) or Ctrl+Shift+I (Windows)
> Inspect Element
- Install AngularJS Batarang extension in Chrome
- Select the element and inspect angular scope
15
Tips on Debugging AngularJS
Place breakpoints at certain lines of code for debug
> Use chrome debugging tools
16
Tips on Debugging AngularJS
Sometimes sass syntax error might cause live-reload failure
> Check on Ionic CLI
> Use jshint in Atom for checking javascript syntax in real time
Common Errors
Reference : AngularJs Error Reference
17
18
Failed to instantiate module…
Solution :
- check for app.js & index.html
- ensure no spelling mistake
- ‘account.ctrlx’ should be ‘account.ctrl’
- ‘account-ctrlx.js’ should be ‘account-ctrl.js’
- or have you accidentally remove the <script> tag but loaded in module?
19
XxxxxCtrl is not a function
Solution :
- check for app.js & index.html
- ensure ‘account.ctrl’ is included as dependency in app.js
- and <script src=“path/to/account-ctrl.js”></script> is in index.html
Cause :
- occurs when you have defined
the controller but did not
include it in app.js/index.html
20
Unknown Provider
Solution :
- check for spelling mistake
- ‘$locatio’ should be ‘$location’
(Mostly happen in controller)
21
$xxxxx is not defined
Solution :
- add it into the .controller() function
Cause :
- usually occurs when use a certain
service without injecting it
- eg : we use $location but didn’t
define it inside .controller() function
22
Unexpected Token }
Cause :
- usually occurs when accidentally add extra comma (,) at the end
of JSON file (eg. in locale JSON file)
Solution :
- Remove the comma (,) at the
last string value
23
CORS Issue When Serving in Desktop Browser
Solution :
- Install CORS extension in chrome
- Remember to turn it on
Note :
- Remember to turn off if didn’t use
- It might affect other page loading
THE END
Thanks for viewing ^^
24

More Related Content

What's hot

Angular 9 New features
Angular 9 New featuresAngular 9 New features
Angular 9 New features
Ahmed Bouchefra
 
Data Flow Patterns in Angular 2 - Sebastian Müller
Data Flow Patterns in Angular 2 -  Sebastian MüllerData Flow Patterns in Angular 2 -  Sebastian Müller
Data Flow Patterns in Angular 2 - Sebastian Müller
Sebastian Holstein
 
Up & running with ECMAScript6
Up & running with ECMAScript6Up & running with ECMAScript6
Up & running with ECMAScript6
Nir Kaufman
 
هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!
Web Standards School
 
An Overview of Angular 4
An Overview of Angular 4 An Overview of Angular 4
Introduction to Angular2
Introduction to Angular2Introduction to Angular2
Introduction to Angular2
Ivan Matiishyn
 
Angular 4 and TypeScript
Angular 4 and TypeScriptAngular 4 and TypeScript
Angular 4 and TypeScript
Ahmed El-Kady
 
Angular2 workshop
Angular2 workshopAngular2 workshop
Angular2 workshop
Nir Kaufman
 
Angular 2 - The Next Framework
Angular 2 - The Next FrameworkAngular 2 - The Next Framework
Angular 2 - The Next Framework
Commit University
 
AngularJS2 / TypeScript / CLI
AngularJS2 / TypeScript / CLIAngularJS2 / TypeScript / CLI
AngularJS2 / TypeScript / CLI
Domenico Rutigliano
 
Dive into Angular, part 5: Experience
Dive into Angular, part 5: ExperienceDive into Angular, part 5: Experience
Dive into Angular, part 5: Experience
Oleksii Prohonnyi
 
Understand components in Angular 2
Understand components in Angular 2Understand components in Angular 2
Understand components in Angular 2
codeandyou forums
 
Introduction To Angular 4 - J2I
Introduction To Angular 4 - J2IIntroduction To Angular 4 - J2I
Introduction To Angular 4 - J2I
Nader Debbabi
 
Android Develpment vol. 2, MFF UK, 2015
Android Develpment vol. 2, MFF UK, 2015Android Develpment vol. 2, MFF UK, 2015
Android Develpment vol. 2, MFF UK, 2015
Tomáš Kypta
 
Request Validation In Spring Rest-Part2
Request Validation In Spring Rest-Part2Request Validation In Spring Rest-Part2
Request Validation In Spring Rest-Part2
Mohammad Sabir Khan
 
Async patterns in javascript
Async patterns in javascriptAsync patterns in javascript
Async patterns in javascript
Ran Wahle
 
Build and deploy an ASP.NET applicaton
Build and deploy an ASP.NET applicatonBuild and deploy an ASP.NET applicaton
Build and deploy an ASP.NET applicatonDavid Voyles
 
The productive developer guide to Angular 2
The productive developer guide to Angular 2The productive developer guide to Angular 2
The productive developer guide to Angular 2
Maurice De Beijer [MVP]
 
What is Angular version 4?
What is Angular version 4?What is Angular version 4?
What is Angular version 4?
Troy Miles
 

What's hot (20)

Coding standards
Coding standardsCoding standards
Coding standards
 
Angular 9 New features
Angular 9 New featuresAngular 9 New features
Angular 9 New features
 
Data Flow Patterns in Angular 2 - Sebastian Müller
Data Flow Patterns in Angular 2 -  Sebastian MüllerData Flow Patterns in Angular 2 -  Sebastian Müller
Data Flow Patterns in Angular 2 - Sebastian Müller
 
Up & running with ECMAScript6
Up & running with ECMAScript6Up & running with ECMAScript6
Up & running with ECMAScript6
 
هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!
 
An Overview of Angular 4
An Overview of Angular 4 An Overview of Angular 4
An Overview of Angular 4
 
Introduction to Angular2
Introduction to Angular2Introduction to Angular2
Introduction to Angular2
 
Angular 4 and TypeScript
Angular 4 and TypeScriptAngular 4 and TypeScript
Angular 4 and TypeScript
 
Angular2 workshop
Angular2 workshopAngular2 workshop
Angular2 workshop
 
Angular 2 - The Next Framework
Angular 2 - The Next FrameworkAngular 2 - The Next Framework
Angular 2 - The Next Framework
 
AngularJS2 / TypeScript / CLI
AngularJS2 / TypeScript / CLIAngularJS2 / TypeScript / CLI
AngularJS2 / TypeScript / CLI
 
Dive into Angular, part 5: Experience
Dive into Angular, part 5: ExperienceDive into Angular, part 5: Experience
Dive into Angular, part 5: Experience
 
Understand components in Angular 2
Understand components in Angular 2Understand components in Angular 2
Understand components in Angular 2
 
Introduction To Angular 4 - J2I
Introduction To Angular 4 - J2IIntroduction To Angular 4 - J2I
Introduction To Angular 4 - J2I
 
Android Develpment vol. 2, MFF UK, 2015
Android Develpment vol. 2, MFF UK, 2015Android Develpment vol. 2, MFF UK, 2015
Android Develpment vol. 2, MFF UK, 2015
 
Request Validation In Spring Rest-Part2
Request Validation In Spring Rest-Part2Request Validation In Spring Rest-Part2
Request Validation In Spring Rest-Part2
 
Async patterns in javascript
Async patterns in javascriptAsync patterns in javascript
Async patterns in javascript
 
Build and deploy an ASP.NET applicaton
Build and deploy an ASP.NET applicatonBuild and deploy an ASP.NET applicaton
Build and deploy an ASP.NET applicaton
 
The productive developer guide to Angular 2
The productive developer guide to Angular 2The productive developer guide to Angular 2
The productive developer guide to Angular 2
 
What is Angular version 4?
What is Angular version 4?What is Angular version 4?
What is Angular version 4?
 

Similar to AngularJs Style Guide

introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
Ravindra K
 
Monorail Introduction
Monorail IntroductionMonorail Introduction
Monorail Introduction
Andrea Magnorsky
 
AngularJS: Overview & Key Features
AngularJS: Overview & Key FeaturesAngularJS: Overview & Key Features
AngularJS: Overview & Key Features
Mohamad Al Asmar
 
Angular 9
Angular 9 Angular 9
Angular 9
Raja Vishnu
 
AngularJS in practice
AngularJS in practiceAngularJS in practice
AngularJS in practice
Eugene Fidelin
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
Nitin Giri
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIAngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPI
Eric Wise
 
Top 10 Mistakes AngularJS Developers Make
Top 10 Mistakes AngularJS Developers MakeTop 10 Mistakes AngularJS Developers Make
Top 10 Mistakes AngularJS Developers Make
Mark Meyer
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
Yakov Fain
 
Suite Script 2.0 API Basics
Suite Script 2.0 API BasicsSuite Script 2.0 API Basics
Suite Script 2.0 API Basics
Jimmy Butare
 
Angular js
Angular jsAngular js
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
Gaurav Agrawal
 
Live Demo : Trending Angular JS Featues
Live Demo : Trending Angular JS FeatuesLive Demo : Trending Angular JS Featues
Live Demo : Trending Angular JS Featues
Edureka!
 
The Basics Angular JS
The Basics Angular JS The Basics Angular JS
The Basics Angular JS
OrisysIndia
 
Angularjs interview questions and answers
Angularjs interview questions and answersAngularjs interview questions and answers
Angularjs interview questions and answers
Anil Singh
 
Working with AngularJS
Working with AngularJSWorking with AngularJS
Working with AngularJS
André Vala
 
Angular best practices
Angular best practicesAngular best practices
Angular best practices
Allanki Srinivas
 
Angular Course.pptx
Angular Course.pptxAngular Course.pptx
Angular Course.pptx
Imdad Ullah
 
London SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingLondon SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error Handling
Richard Clark
 
Introduction to Angular Js
Introduction to Angular JsIntroduction to Angular Js
Introduction to Angular Js
Professional Guru
 

Similar to AngularJs Style Guide (20)

introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
 
Monorail Introduction
Monorail IntroductionMonorail Introduction
Monorail Introduction
 
AngularJS: Overview & Key Features
AngularJS: Overview & Key FeaturesAngularJS: Overview & Key Features
AngularJS: Overview & Key Features
 
Angular 9
Angular 9 Angular 9
Angular 9
 
AngularJS in practice
AngularJS in practiceAngularJS in practice
AngularJS in practice
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIAngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPI
 
Top 10 Mistakes AngularJS Developers Make
Top 10 Mistakes AngularJS Developers MakeTop 10 Mistakes AngularJS Developers Make
Top 10 Mistakes AngularJS Developers Make
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
 
Suite Script 2.0 API Basics
Suite Script 2.0 API BasicsSuite Script 2.0 API Basics
Suite Script 2.0 API Basics
 
Angular js
Angular jsAngular js
Angular js
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
Live Demo : Trending Angular JS Featues
Live Demo : Trending Angular JS FeatuesLive Demo : Trending Angular JS Featues
Live Demo : Trending Angular JS Featues
 
The Basics Angular JS
The Basics Angular JS The Basics Angular JS
The Basics Angular JS
 
Angularjs interview questions and answers
Angularjs interview questions and answersAngularjs interview questions and answers
Angularjs interview questions and answers
 
Working with AngularJS
Working with AngularJSWorking with AngularJS
Working with AngularJS
 
Angular best practices
Angular best practicesAngular best practices
Angular best practices
 
Angular Course.pptx
Angular Course.pptxAngular Course.pptx
Angular Course.pptx
 
London SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingLondon SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error Handling
 
Introduction to Angular Js
Introduction to Angular JsIntroduction to Angular Js
Introduction to Angular Js
 

AngularJs Style Guide

  • 3. Style Guide Reference : JohnPapa & mgechev 3
  • 4. 4 Single Responsibility [Style Y001] Avoid putting app modules, controllers, factories in the same file Recommended 1 Component Per File
  • 5. 5 IIFE [Style Y010] Wrap angular module inside immediately invoke function expression (IIFE) Why? An IIFE removes variables from the global scope. This helps prevent variables and function declarations from living longer than expected in the global scope, which also helps avoid variable collisions. Why?: When your code is minified and bundled into a single file for deployment to a production server, you could have collisions of variables and many global variables. An IIFE protects you against both of these by providing variable scope for each file. Strict mode : cannot use undeclared variable
  • 6. 6 Definition of Modules (Setters) [Style Y021] Avoid using a variable name when declare module Why? With 1 Component Per File, there is rarely a need to introduce a variable for the module Recommended to use a simpler setter syntax
  • 7. 7 Getters [Style Y022] Avoid using a variable name when using module Recommended to use chaining with the getter syntax Why? This produces more readable code and avoids variable collisions or leaks
  • 8. 8 controllerAs view syntax [Style Y030] Avoid using classic controller with $scope syntax Use the controllerAs syntax Why?: It promotes the use of binding to a "dotted" object in the View (e.g. customer.name instead of name), which is more contextual, easier to read, and avoids any reference issues that may occur without "dotting".
  • 9. 9 controllerAs with “self” [Style Y032] *** NOTE : In Our Project, use “self” instead of “vm” for consistency Use a capture variable for “this” when using the controllerAs syntax. Choose a consistent variable name such as vm, which stands for ViewModel var self = this; self.name = {}; self.sendMessage = function() {};
  • 10. 10 Function Declarations to Hide Implementation Details [Style Y034] Why?: You never have to worry with function declarations that moving var a before var b will break your code because a depends on b. Why?: Move the complexity out of view so can see important stuff up top Why?: Easier to read and identify which function can be bound to view Why?: Order is critical with function expressions example of function expression : var a = function() { }; var b = function() { };
  • 11. 11 Defer Controller Logic to Services [Style Y035] Why?: Logic may be reused by multiple controllers when placed within a service and exposed via a function. Why?: Removes dependencies and hides implementation details from the controller.
  • 12. 12 Order of Dependency Injection Put angular built in service at the front and self-defined service at the back Why?: Improve readability and consistency <input type="text" ng-model=“{{name}}” my-directive=“{{data}}” />
  • 14. 14 Tips on Debugging AngularJS > Use scope inspection tools Always press command+option+I (Mac) or Ctrl+Shift+I (Windows) > Inspect Element - Install AngularJS Batarang extension in Chrome - Select the element and inspect angular scope
  • 15. 15 Tips on Debugging AngularJS Place breakpoints at certain lines of code for debug > Use chrome debugging tools
  • 16. 16 Tips on Debugging AngularJS Sometimes sass syntax error might cause live-reload failure > Check on Ionic CLI > Use jshint in Atom for checking javascript syntax in real time
  • 17. Common Errors Reference : AngularJs Error Reference 17
  • 18. 18 Failed to instantiate module… Solution : - check for app.js & index.html - ensure no spelling mistake - ‘account.ctrlx’ should be ‘account.ctrl’ - ‘account-ctrlx.js’ should be ‘account-ctrl.js’ - or have you accidentally remove the <script> tag but loaded in module?
  • 19. 19 XxxxxCtrl is not a function Solution : - check for app.js & index.html - ensure ‘account.ctrl’ is included as dependency in app.js - and <script src=“path/to/account-ctrl.js”></script> is in index.html Cause : - occurs when you have defined the controller but did not include it in app.js/index.html
  • 20. 20 Unknown Provider Solution : - check for spelling mistake - ‘$locatio’ should be ‘$location’ (Mostly happen in controller)
  • 21. 21 $xxxxx is not defined Solution : - add it into the .controller() function Cause : - usually occurs when use a certain service without injecting it - eg : we use $location but didn’t define it inside .controller() function
  • 22. 22 Unexpected Token } Cause : - usually occurs when accidentally add extra comma (,) at the end of JSON file (eg. in locale JSON file) Solution : - Remove the comma (,) at the last string value
  • 23. 23 CORS Issue When Serving in Desktop Browser Solution : - Install CORS extension in chrome - Remember to turn it on Note : - Remember to turn off if didn’t use - It might affect other page loading
  • 24. THE END Thanks for viewing ^^ 24