Angular.js 1.x

ng-app tells this div belongs to this angular app.

You can bind to html class attributes too!

<input type="text" ng-model="data.message">
<div class="">


All element references in Angular are always wrapped with jQuery or jqLite; they are never raw DOM references.


How to pass parameter?

<div enter="panel" leave>I'm content</div>
app.directive("leave", function () {
  return function (scope, element, attrs) {
    element.bind("mouseleave", function () {
  • During the linking phase, interpolation has not been evaluated yet, so if the value of a directive contains { { } }, it is necessary to call attrs.$observe() in order to properly evaluate the value, otherwise it will return as undefined.
  • They share attri object, which is really useful in directive (


Using service('name', function) or factory('name', function)?

They are all singleton: initizlied ONLY once.

  • Services: When declaring serviceName as an injectable argument you will be provided with an instance of the function. In other words new FunctionYouPassedToService().
  • Factories: When declaring factoryName as an injectable argument you will be provided with the value that is returned by invoking the function reference passed to module.factory.


Do not use controllers for:

  • Any kind of DOM manipulation — Controllers should contain only business logic. DOM manipulation—the presentation logic of an application—is well known for being hard to test. Putting any presentation logic into controllers significantly affects testability of the business logic. Angular offers databinding for automatic DOM manipulation. If you have to perform your own manual DOM manipulation, encapsulate the presentation logic in directives.
  • Input formatting — Use angular form controls instead.
  • Output filtering — Use angular filters instead.
  • Sharing stateless or stateful code across controllers — Use angular services instead.
  • Managing the life-cycle of other components (for example, to create service instances).


A directive is just a function which executes when the compiler encounters it in the DOM.

Define directive using camel cased names such as ngBind. Write directive in html by translating the camel case name into snake case with these special characters :, -, or _.

Directive can talk to each other via other's controllers injected in link function through require

What's the difference between @ and =?


Filter should do text manipulation ONLY. If you want to add color, use directive.

Available Filters: json (convert js object to json string), limitTo (return a new array) ?? why the logic is not in controller?


When and why?

Directives Talking to Controllers, use scope.apply('method name on the scope()')


Where to put 'view did load' init functions?
Put in controller init code. See

results matching ""

    No results matching ""