Angular 1.x
component based model
Why Angular?
File: home.jsp
<jsp:include src=”../header.jsp” />
<jsp:include src=”../content.jsp” />
<jsp:include src=”../footer.jsp” />
Every time rerenders or reloads a page for major changes in view.
SPA [ Single page application ]
Modules
Controller
Constructor function that defines:
→ sets the initial state of $scope object
→ adds behaviour to $scope object
Services / factories
There are the Object that can be used across the app injection.
→ Lazily instantiated
→ Singleton
Provider
Much more similar to services but allows us to configure on app configuration phase.
Service vs Factory
Config
Used to perform an action before angular module gets loaded.
→ Useful when we want to configure our provider before it returns a factory instance
Run
Used to perform an action after angular module gets loaded.
→ Useful when we want to do some initialization operation like fetching data from server before it renders view.
Scope
Is an object (model) that defines the execution context of angular expression
Scope lifecycle
Thing that create new scope
Inherited scope
Scope inheritance works based on prototypical inheritance.
Child scope property aString hides access to parent scope
NgModel [ Binding ]
Model value
View value
Magic
NgModelController
Component
Example
File : todos.html → DOM structure
<input type=”text” ng-model=”todo” />
<todos>
<todo> </todo>
<todo> </todo>
</todos>
Dependency Injection
Dependency Injection (DI) is a software design pattern that deals with how components get hold of their dependencies.
There are only 3 ways a component can depends on other component:
1. Create dependency with new keyword
2. Referring a global variable
3. Passing dependency as argument
Injector
Ways to define a dependency in Angular
→ Dependency Annotation
1. Inline
2. Using $inject property
3. As a function parameter