...
Expressions
- Expressions are similar to like the result of an eval(javascript) (roughly). They are processed by Angular and, therefore, have these important, distinct properties:
- All expressions are executed in the context of the scope and have access to local $scope variables.
- They do not throw errors if an expression results in a TypeError or a ReferenceError.
- They do not allow for any control flow functions (conditionals; e.g., if/else).
- They can accept a filter and/or filter chains
- Expressions are similar to like the result of an eval(javascript) (roughly). They are processed by Angular and, therefore, have these important, distinct properties:
Controllers
- The controller in AngularJS is a function that adds additional functionality to the scope of the view. They are used to set up an initial state and to add custom behavior to the scope object.
- To create custom actions we can call in our views, we can just create functions on the scope of the controller.
Services
- Services provide a method for us to keep data around for the lifetime of the app and communicate across controllers in a consistent manner.
- Services are singletons objects that are instantiated only once per app (by the $injector) and lazy-loaded (only created when necessary). They provide an interface to keep together methods that relate to a specific function.
The most common and flexible way to create a service uses the angular.module API factory:
Code Block | ||||
---|---|---|---|---|
| ||||
// Example service that holds on to the current_user for the lifetime of the app
angular.module('myApp.services', [])
.factory('UserService', ['$http', function($http) {
var current_user;
return {
getCurrentUser: function() {
return current_user;
},
setUsername: function(user) {
current_user; = user;
}
}
}]);
|
Building
TODO: add content here
...
Data Binding
- Due to the nature of javascript itself and how it passes by value vs. reference, it's considered a best- practice in Angular to bind references in the views by an attribute on an object, rather than the raw object itself.
Services
- If you want your function to be called like a normal function, use factory. If you want your function to be instantiated with the new operator, use service. If you don't know the difference, use factory. Service or Factory?
...