Understanding AngularJS Flashcards

1
Q

Why is AngularJS MV*

A

AngularJS is MV* because of the binding between the Model and the View, using either controllers, or the model view. The * represent whatever because there are various ways of binding the model and the view, meaning whatever happens in the Model, automatically affects the View, and vice versa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an attribute?

A

An attribute is a modifier of an html element, that provides more information about an html element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an angular controller?

A

An angular controller is a javascript constructor function that controls the flow of data in an angular application. It is the glue that sticks the model(data) and the view(html)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe dependency injection in javascript

A

Dependency injection is a process of passing an object to a function, instead of defining the object inside a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Illustrate dependency injection

A
var Animal = function(type, sex) {
    this.type = type;
    this.sex = sex;
}
function logAnimal(animal) {
  console.log(animal);
}

var tiger = new Animal(‘tiger’, ‘male’);

logAnimal(tiger); // passing tiger object to logAnimal function

  • The logAnimal function is dependent on the animal object to be able to log it, and hence we are injecting the dependency, which is the animal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the $scope?

A

The $scope is a service object that is injected into the controller, and binds the controller data and the view. It sits in between the view and the controller

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does dependency injection work in angular?

A

Dependency injection works through JavaScript’s ability to pass a function as a string. When a function is passed as an object, the function arguments are converted by angular into a string array, with each argument representing the array element. Angular then reads each string element and creates an object based on the string value. For example, if angular sees ‘$scope’, it knows to create the $scope object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Illustrate how a controller definition survives minification

A

myApp.controller(‘mainController’, [‘$scope’, ‘$log’, function($scope, $log) {
} ] )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is interpolation?

A

Interpolation is the process of creating a string by combining a string and a variable, e.g. ‘Hello’ + name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does interpolation work in angular?

A

Interpolation works by defining variables in the controller, passing values to them, and then injecting the variable values in the view using double curly braces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give an example of interpolation in angular

A

myApp.controller(‘mainController’, [‘$scope’, function($scope){
$scope.name = ‘Saki’;
}])

<h1>Hello {{ name }}</h1>

// displays Hello Saki

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a directive?

A

A directive is an instruction to angular to manipulate a piece of the dom, eg. add a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are some examples of angular directives?

A

ng-model - specifies which scope variable the view element is bound to
ng-controller, ng-app

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an angular watcher?

A

An angular watcher is metaphor which describes the action that angular takes to monitor any changes made to a $scope object variable. Whenever you create a data binding in your view, on a $scope object variable, angularjs automatically creates a “watch” for that variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How are watches created in angular

A

Watches are created using the $scope.$watch() function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain how $scope.$watch() works

A

$scope.$watch() watches a particular $scope object variable. $scope.$watch() take two parameters, function value/scope model and listener function. When it runs, it compares the current function/model value with a previous function/model value, and if there is a difference, the listener function is called

17
Q

Explain how $scope.$digest() works

A

$scope.$digest() iterates through all watchers in the $scope object. As it iterates over them, it interrogates the values returned by the watchers, comparing them with previous values, and if there are differences, it calls the listener function

18
Q

Explain how $scope.$apply() works

A

$scope.$apply() takes a function as a parameter, which is executed, and after execution, the $scope.$digest is called

19
Q

What is a singleton

A

A singleton is the one and only copy of an object. In angular, all services are singletons are services, except for $scope

20
Q

What is the advantage of using services in angular?

A

Angular services are singletons, and this enables content sharing across ‘pages’

21
Q

In angularjs, what does it mean to bootstrap?

A

In angularjs, bootstraping means initializing, or starting, your angularjs app.

22
Q

Name two ways to bootstrap your angular app

A

Two ways to bootstrap your angularjs app are:

  1. automatic bootstraping - achieved by including ng-app to an html element i.e.
  2. use javascript, angular.bootstrap(document, [‘myApp’])
23
Q

What is normalization

A

Normalization is the process in which angular converts an attribute’s name to match with a directive, eg, ng-model is converted to ngModel

24
Q

What is a module?

A

A module is a container that contains angular components such as services, controllers, etc

25
Q

What is a controller?

A

A controller acts as the brain of the view, it interacts with factories/services to retrieve some data, and bind the data into the view using the $scope

26
Q

What is a factory?

A

A factory is a custom object, used to

  1. define re-usable tasks
  2. share data or state between controllers
27
Q

What are the differences between a factory and a service?

A

The differences between a factory and a service are:

  1. a service is instantiated, therefore you need to use this . On the other hand, a factory is not instantiated, you need to return a custom object, which is accessible outside of the factory
  2. you use the service keyword to attach a service to the main module, and the factory keyword to attach a factory
28
Q

What is scope isolation?

A

Scope isolation is the process of defining an isolated model for a directive, meaning the directive will not have access to its parent’s scope, and the parent will not have access to the directive’s scope.

29
Q

How do you isolate a directive’s scope?

A

By defining a scope object

scope : {
}

30
Q

What is the compile function?

A

Compile is an angular function defined for a directive, that can be called, and passed the original DOM as an argument. You can manipulate the DOM before it is instantiated

31
Q

What is the link function?

A

Link is an angular function defined for a directive, that can be called, passed an instantiated DOM, scope, as well as attributes as arguments

32
Q

What does the ngInclude directive do?

A

The ngInclude directive enables you to conditionally include templates, pieces of AngularJS-infused HTML,
into the page, based on the JavaScript state

33
Q

which main method is exposed by the $routeProvider

A

the $routeProvider exposes:
when - used to map URLs. It takes 2 parameters
a. url
b. an object, which takes its own parameters,
such as templateUrl, controller, resolve,
redirectTo, etc

34
Q

How is ngInclude useful?

A

To avoid facing CORS errors, you can use ngInclude to load files from the same domain