Skip to content

Instantly share code, notes, and snippets.

@DanisHack
Created June 12, 2014 04:02
Show Gist options
  • Save DanisHack/7ddb09eb7b05579ebeec to your computer and use it in GitHub Desktop.
Save DanisHack/7ddb09eb7b05579ebeec to your computer and use it in GitHub Desktop.
Angular Directive Learning
Directive Learnings:
*) Make the changes in the compile function, not link function if
You want to change the template.
*) If, however, you want to change the instances, make change in link function.
- Above 2 Mostly relevant for ng-repeat -
*) No scope available in compile function
*) Scope vaiables bound in the linking function will only work the first time.
- To constantly update as vaiable changes, use scope.watch.
*) scope.apply can be used to update scope variables from a bound event (like click)
to update the scope and let angular know. Basically, if you are using other libs
(jquery, Dom events) etc and want to inform angular of any changes, use apply..
*) TRICK - Print Error().stack and if there's no apply in the stack trace, you are
outside of angular land. To get back in, use apply.
*) Transclude will create a scope that is sibling to the directive it's in and will
inherit the directive's parent's scope
*) Use Scope.watch for scope properties and attrs.$observe to
observe changes to "interpolated" attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment