- An execution context for expressions
- An object which is a reference to the application model
- It binds the view to the controller
- Instanciated by the $rootScope
- Scopes are arranged in hierarchical structure which mimic the DOM structure of the application
- Scopes can watch expressions and propagate events.
- It has the methods: $on, $emit, $broadcast, $apply, $applyAsync, $digest, $destroy, $new, $watch, $watchGroup, $watchCollection, $eval, $evalAsync
- Allow to manipulate the DOM in 4 possible ways (link, transclude function, $compile and controller)
- There is 4 types: Element, Attribute, Class and Comment. But best practice is only to use the 2 firsts.
- Can have an isolated Scope or not
- If we need more than just manipulate the DOM, we can add a Controller to have some logic
- Avec: link (arguments: scope, element, attrs, ctrl, transcludeFN of the DOM node)
- Best practice: Don't use Controller, only use Directives with Controllers inside of them.
- Is instanciated with .factory('NameOfTheService', function([Dependencies]) {[Code here]}); method
- .factory() is not configurable
- .constant() is configurable (declare a constant in your app like your server root URL for API calls)
- Is a Singleton (design pattern) accessible by the Dependency Injection (DI)
- Is used to organize and share code across the application
- Attached to a DOM element with the Directive ng-controller (see ngController on the Doc)
- Used to augment the Scope of Angular
- Allow to setup the inital state of the $scope object
- Add behavior to the $scope object
- Angular will instantiate a new Controller object, using the specified Controller's constructor function.
- New child scope will be created and made available as an injectable parameter to the Controller's
- If the controller has been attached using the "controller as" syntax then the controller instance will be assigned to a property on the new scope.
- Replace JS callbacks
- Easier to use and to follow the code logic flow. Functions are cleaner.
- 99% times used inside a Directive
- Lib promise: Bluebird (recommanded with NodeJS)
- $q is Angular's promise lib (fork of the "q" lib )
- A function is said 'then-able' when can use '.then' method instead of callbacks
- $resource & $http are Promises (used to retrive and send data to an API endpoint for exemple)