I’ll be using AngularJS for a project at work, mainly using Mastering Web Application Development with AngularJS and This ebook.
Basic Concepts
Model
AngularJS models are plain, old JS objects. Models properties are not limited to be represented by primitive values(any valid JS object or array can be used). To expose a model to AngularJS you simply assign it to a $scope.
Controller
In AngularJS, a controller is just a regular JavaScript constructor which accepts a magic $scope parameter.
The primary responsibility of a controller is to initialize scope objects.
- Providing initial model values
- Augmenting $scope with UI-specific behavior/functions
- A controller does the same job as the ng-init directive, but doing so in JS not HTML, when it comes to setting up initial model values.
Service & Dependency Injection
- Think of Service as the method of registering a constructor function/any singleton object that is created and managed by angular DI, with service dependency injection we can eliminate the new keyword
- Services are singleton. All components of application works with single instance of the service.
- Service instance gets created when application components need it.
- Provides organization of shared data and functions across the application.
Service can be created in four different ways:
- Using the service() method
- Using the factory() method
- Using the provider() method
- Using the value() method
- Using the constant() method
Using ervice()
Using the service() method uses the function constructor and it returns the object or instance of the function to work with
creating a calculator service
|
|
|
|
|
|
Using factory()
create factory object and assign the methods to it.
Using the factory() method uses the returned value of the function. It returns the value of the function returned after the execution
Creating the service to reverse the string.
|
|
|
|
The difference between creating a service using the service() method and the factory() method:
- Using the service() method uses the function constructor and it returns the object or instance of the function to work with
- Using the factory() method uses the returned value of the function. It returns the value of the function returned after the execution
(http://stackoverflow.com/questions/13762228/confused-about-service-vs-factory)
Well known services
$http and $q
$http in AngularJS is a core service for reading data from web servers. $http.get(url) is the function to use for reading server data.
|
|
$resource
$document
$window
$timeout & $interval
(http://stackoverflow.com/questions/14237070/using-setinterval-in-angularjs-factory)
(http://tutorials.jenkov.com/angularjs/timeout-interval.html#injecting-interval)
(http://plnkr.co/edit/?p=preview)
$parse
$cacheFactory
$filter
$apply
$watch & $watchcollection
(http://jsfiddle.net/TheSharpieOne/RA2j7/2/)
Directive
Simple program
I’ve build a simple one-page website using angular-ui and .NET framework.
|
|
studentCtrl.js
|
|
Default.aspx
|
|
Site.Master
|
|