Skip to content

Instantly share code, notes, and snippets.

@ohvitorino
Last active July 13, 2016 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohvitorino/1f73ac326553ed99246d09c32761250b to your computer and use it in GitHub Desktop.
Save ohvitorino/1f73ac326553ed99246d09c32761250b to your computer and use it in GitHub Desktop.
AngularJS 1.5 Best practices and Notes

Project generation and structure

  • Angular-seed repo
  • Yeoman angular (strongly advised). Provides commands to generate: directives, controllers, views

ng-cloak attribute

Use ng-cloak directive to hide interpolation tags {{}} in index.html

Ex: <body ng-cloack>

And use ng-bind instead of interpolation to avoid showing {{}}. Ex: <span ng-bind="ctrl.value">?</span>

#Use minification? Maybe not... many times this is not necessary

Directives

The use of prefixes might not work in IE. Use instead <div app-blinkdirective>

Put DOM manipulation in directives

Controllers

Don't put DOM in the controllers

Services

No DOM

Singletons

Independent of view

Use for cache. Services exist for the hole lifecycle of the application

$scope

READ ONLY in templates

WRITE ONLY in controllers

scope is not the model

ng-model if don't have a dot, you are doint it wrong

Code tips

angular.extend(obj1, obj2) to extend objects

angular.isObject() to check if a variable is an object

angular.isArray() to check if a variable is an array

angular.forEach(data, function (value, key) { ... }) enumerating an object's properties

"property1" in myData Check if object has a property

angular.module(moduleName).constant(name, value) Creates a new constant in a module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment