-
Notifications
You must be signed in to change notification settings - Fork 27.5k
FAQ
Editors: put 2 spaces at the end of a question to create a <br>
(There's a new Testing section below)
-
Where can I find good learning resources?
Learning Resources -
How do I access the DOM from a controller?
DO NOT perform DOM selection/traversal from the controller. The HTML hasn't rendered yet. Look up 'directives'. -
Why does angular say my controllers/directives/etc are missing?
Callingangular.module('myApp', [])
will ALWAYS create a new module (and wipe out your existing one). Instead, make sure to callangular.module('myApp')
with only 1 parameter to refer to an already created module. -
How to render unescaped data?
You will probably want ng-bind-html-unsafe sooner or later. -
How can I watch when an array/object/ngResource query is modified?
$scope.$watch
has a third parameter to monitor changes by value (and not by reference). -
Don't try to serialize the form or collect the input values manually. Just slap
ng-model="data.myField"
onto every form input you use and then take a gander at$scope.data
when you finally need it. -
Always have a '.' in your ng-models. Misko best practice.
-
$rootScope is essentially where you put ng-app. You can inject $rootScope into your bootstrap or services to add stuff that should be accessible on all scopes.
-
The difference between
module().factory()
andmodule().service()
. -
Prevent Flash Of Unstyled Content (FOUC) (and curly braces) by mixing
ng-bind
withng-cloak
. -
Nested Routes / Views? Maybe...
-
You can always do
<script id="some/partial.html" type="text/ng-template">
and angular will use it instead! -
Escape the port in
$resource('example.com\\:8080')
-
Angular watches the input event, not the 'change' event.
-
Don't use jQuery to toggle crap. Just use a lot of variable flags inline:
<a ng-click="flags.open=!flags.open">...<div ng-class="{active:flags.open}">
-
If you're on Google Chrome, install the Batarang extension, inspect a DOM element, and type
$scope
in the console. -
Checkout AngularUI for an awesome collection of directives (and even BETTER example code).
-
For IE v8.0 or earlier you may want to read this and use this.
-
If you want to get rid of the
#
in your routes, search the docs forhtml5mode
. -
You should try using the AngularUI Passthru Directive (uiJq) before trying to roll your own jQuery plugin wrapper directive.
-
If you change
newVal
inside your$scope.$watch
it could fire again (recursively?). -
You should ONLY use
$scope.$apply
in non-angular events/callbacks. It usually doesn't belong anywhere else. -
Bypass HTML5Mode for
<a href>
by addingtarget="_self"
-
How do I
.preventDefault()
or.stopPropagation()
?
Allng-click
and related bindings inject a$event
object that you can call things like.preventDefault()
or even pass the object to your methods -
AngularJS doesn't work in my Chrome extension!
You want to useng-csp
-
Rejecting / Resolving a
$q.defer()
doesn't go through
You must add a$scope.$apply()
for these to process -
Jasmine
spyOn()
is not executing the spy'd function
Not necessarily an AngularJS question, but you need to append.andCallThrough()
-
How do I test async code?
Focus on creating mocks (fake objects / functions) that let you flush the async stuff synchronously.$timeout
has a.flush()
method specifically for this reason, just remember to add a$scope.$apply()
too. If that still doesn't work, look up Jasmine'sruns()
andwaits()
functions.