Angular JS study set

What is AngularJS?

AngularJS is a javascript framework used for creating single web page applications. It allows you to use HTML as your template language and enables you to extend HTML's syntax to express your application's components clearly
AngularJS is cross-browser compliant
AngularJS is open source, completely free, and used by thousands of developers around the world. It is licensed under the Apache License version 2.0.

The key features of AngularJS are

Scope
Controller
Model
View
Services
Data Binding
Directives
Filters
Testable

what is scope in AngularJS ?

Scope refers to the application model, it acts like glue between application controller and the view. Scopes are arranged in hierarchical structure and impersonate the DOM ( Document Object Model) structure of the application. It can watch expressions and propagate events.

what is services in AngularJS ?

In AngularJS services are the singleton objects or functions that are used for carrying out specific tasks. It holds some business logic and these function can be called as controllers, directive, filters and so on.

What is Angular Expression? Explain what is key difference between angular expressions and JavaScript expressions?

Like JavaScript, Angular expressions are code snippets that are usually placed in binding such as {{ expression }}
The key difference between the JavaScript expressions and Angular expressions
Context : In Angular, the expressions are evaluated against a scope object, while the Javascript expressions are evaluated against the global window
Forgiving: In Angular expression evaluation is forgiving to null and undefined, while in Javascript undefined properties generates TypeError or ReferenceError
No Control Flow Statements: Loops, conditionals or exceptions cannot be used in an angular expression
Filters: To format data before displaying it you can use filters

ng-app

Specifies what Angular app the HTML contained within will function under
Also Defines the root element of an Application
You can only have one ng-app directive in your HTML document. If more than one ng-app directive appears, the first appearance will be used.

ng-bind

The ngBind attribute tells Angular to replace the text content of the specified HTMLelement With the value of a given expression, and to update the text content when the value of that expression changes.
Typically, you don't use ngBind directly, but instead you use the double curly markup like {{ expression}} which is similar but less verbose.
It is preferable to use ngBind instead of {{expression}} if a template is momentarily displayed by the browser in its raw state before Angular compiles it. Since ngBind is an element attribute, it makes the bindings invisible to the user while the page is loading.
An alternative solution to this problem would be using the ngCloak directive.

Explain what is data binding in AngularJS

Data binding in AngularJS binds AngularJS expressions with AngularJS data.
Automatic synchronization of data between the model and view components is referred as data binding in AngularJS. There are two ways for data binding
Data mining in classical template systems
Data binding in angular templates

ng-init

e.g <div ng-app="" ng-init="names=['Jani','Hege','Kai']">
We can initialize data with ng-init so that we can use some quick data.
The ng-init directive defines initial values for an AngularJS application.
Normally, you will not use ng-init. You will use a controller or module instead.

What can ng-model bind to?

HTML controls (input, select, textarea)

Explain what are directives ? Mention some of the most commonly used directives in AngularJS application ?

A directive is something that introduces new syntax, they are like markers on DOM element which attaches a special behavior to it. In any AngularJS application, directives are the most important components.
Some of the commonly used directives are ng-model, ng-App, ng-bind, ng-repeat , ng-show etc.

What are the advantages of using AngularJS ?

AngularJS has several advantages in web development.
1. AngularJS supports MVC pattern
2. Can do two ways data binding using AngularJS
3. It has per-defined form validations
4. It supports both client server communication
5. It supports animations

Explain what Angular JS routes does ?

Angular js routes enable you to create different URLs for different content in your application. Different URLs for different content enables user to bookmark URLs to specific content. Each such bookmarkable URL in AngularJS is called a route
A value in Angular JS is a simple object. It can be a number, string or JavaScript object. Values are typically used as configuration injected into factories, services or controllers. A value should be belong to an AngularJS module.
Injecting a value into an AngularJS controller function is done by adding a parameter with the same name as the value

Explain what is string interpolation in Angular.js ?

In Angular.js the compiler during the compilation process matches text and attributes using interpolate service to see if they contains embedded expressions. As part of normal digest cycle these expressions are updated and registered as watches.

Mention the steps for the compilation process of HTML happens?

Compilation of HTML process occurs in following ways
Using the standard browser API, first the HTML is parsed into DOM
By using the call to the $compile () method, compilation of the DOM is performed. The method traverses the DOM and matches the directives.
Link the template with scope by calling the linking function returned from the previous step

ng�repeat and it's variables

$index $first $middle $last $even $odd

ng-switch example and key word

<div ng-switch="myVar">
<div ng-switch-when="dogs">

ng-option example

<select ng-model="selectedName" ng-options="item for item in names"></select>

When would you want to use a config()?

So you would have to use .config() when you want to change configuration of a ngCore module which has already been written in angular.js or if you want to change configuration of a third party library which your app uses.

What is the difference between Factory and service

Factory will return an object or a value from it's inner object. And service is a constructor which uses "this" keyword to create function and propriety for different controller to use.

<meta name="viewport" content="width=device-width, initial-scale=1.0">
Explain each component of this tag

A <meta> viewport element gives the browser instructions on how to control the page's dimensions and scaling.
The width=device-width part sets the width of the page to follow the screen-width of the device (which will vary depending on the device).
The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser.

What are the states of input?

$untouched The field has not been touched yet
$touched The field has been touched
$pristine The field has not been modified yet
$dirty The field has been modified
$invalid The field content is not valid
$valid The field content is valid

What are the states of form?

$pristine No fields have been modified yet
$dirty One or more have been modified
$invalid The form content is not valid
$valid The form content is valid
$submitted The form is submitted

Explain $watch()

he $scope.watch() function creates a watch of some variable. When you register a watch you pass two functions as parameters to the $watch() function:
A value function
A listener function

Explain $apply()

The $scope.$apply() function takes a function as parameter which is executed, and after that $scope.$digest() is called internally. That makes it easier for you to make sure that all watches are checked, and thus all data bindings refreshed.

Explain $digest()

The $scope.$digest() function iterates through all the watches in the $scope object, and its child $scope objects (if it has any). When $digest() iterates over the watches, it calls the value function for each watch. If the value returned by the value function is different than the value it returned the last time it was called, the listener function for that watch is called.

When do we need to call $apply() manually then?

The better practice is to call $apply only when you know you are outside of a $digest loop, such as inside the directive link function.

what are the characteristics of "Scope"?

To observer model mutations scopes provide APIs ($watch)
To propagate any model changes through the system into the view from outside of the Angular realm
A scope inherits properties from its parent scope, while providing access to shared model properties, scopes can be nested to isolate application components
Scope provides context against which expressions are evaluated

Explain what is DI (Dependency Injection ) and how an object or function can get a hold of its dependencies ?

DI or Dependency Injection is a software design pattern that deals with how code gets hold of its dependencies. In order to retrieve elements of the application which is required to be configured when module gets loaded , the operation "config" uses dependency injection.
These are the ways that object uses to hold of its dependencies
Typically using the new operator, dependency can be created
By referring to a global variable, dependency can be looked up
Dependency can be passed into where it is required

Explain the concept of scope hierarchy? How many scope can an application have?

Each angular application consist of one root scope but may have several child scopes. As child controllers and some directives create new child scopes, application can have multiple scopes. When new scopes are formed or created they are added as a children of their parent scope. Similar to DOM, they also creates a hierarchical structure.

Difference between sessionStorage, cookies, and localStorage

SessionStorage - The data is stored for a particular session. The data will be lost whenever the browser tab will be closed or after some particular session. Maximum size stored can be up to 5MB.
LocalStorage - The data is stored with no expiration date. The data can only be cleared by JavaScript or by clearing the browser cache. Storage limit is maximum than the sessionStorage and cookie.
Cookies - It stores the data that has to be sent back to the server with some requests. The cookie's expiration varies on the type and duration set from either the server side or client side. Maximum size stored can be less than 4KB.

What is the role of $routeProvider in AngularJs?

It is the $routeProvider that helps in navigating between different pages/links without separately loading the page/link whenever a user clicks on a link.
ngRoute config() method is used to configure the routeProvider.

What is the difference between $scope and scope?

In AngularJs $scope is used to achieve dependency injection and scope is used for linking between View (i.e HTML) and Controller (i.e JS).

How are AngularJs prefixes $ and $$ used?

$$ variable in AngularJS is used as a private variable, as it is used to prevent accidental code collision with the user code.
Whereas $ prefix can be used to denote angular core functionalities (like a variable, parameter, property or method).

Select a HTML element using angular

angular.element(document.querySelector('CSSselectors'))

Is Nested Controllers possible or not in AngularJs?

Yes, it is possible as Nested Controllers are well-defined in a classified manner while using a view.

If both factory and service are equivalent, then when should I use them?

Factory provider is preferred using an object, whereas a service provider is preferred using with class.

$sce

Strict Contextual Escaping (SCE) is a mode in which AngularJS constrains bindings to only render trusted values. Its goal is to assist in writing code in a way that (a) is secure by default, and (b) makes auditing for security vulnerabilities such as XSS, clickjacking, etc. a lot easier.

$q,defered,promise

Promise is an interface which represents a proxy value. ie. We don't know the value at the time it's created. This promise will return value like any synchronous function in future. There can be several states for a promise.
Promises in AngularJS is provided by $q service. It has reach feature like any other Promise library. $q service is a service that helps you to run your code asynchronously. As its documentation say, this is inspired by Chris Kowal's Q library (github.com/kriskowal/q).
Deferred Object:
Deferred is an object which exposes the promise. It has mainly three methods resolve(), reject(), and notify(). Deferred returns promise object. When Deferred completes, You call methods either resolve(), reject(), and notify() . It calls callback register to either resolve(), reject(), or notify() according to how it has completed.

Bootstrapping

...