Understanding AngularJS Flashcards
Why is AngularJS MV*
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
What is an attribute?
An attribute is a modifier of an html element, that provides more information about an html element
What is an angular controller?
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)
Describe dependency injection in javascript
Dependency injection is a process of passing an object to a function, instead of defining the object inside a function
Illustrate dependency injection
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
What is the $scope?
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 does dependency injection work in angular?
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
Illustrate how a controller definition survives minification
myApp.controller(‘mainController’, [‘$scope’, ‘$log’, function($scope, $log) {
} ] )
What is interpolation?
Interpolation is the process of creating a string by combining a string and a variable, e.g. ‘Hello’ + name
How does interpolation work in angular?
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
Give an example of interpolation in angular
myApp.controller(‘mainController’, [‘$scope’, function($scope){
$scope.name = ‘Saki’;
}])
<h1>Hello {{ name }}</h1>
// displays Hello Saki
What is a directive?
A directive is an instruction to angular to manipulate a piece of the dom, eg. add a class
What are some examples of angular directives?
ng-model - specifies which scope variable the view element is bound to
ng-controller, ng-app
What is an angular watcher?
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 are watches created in angular
Watches are created using the $scope.$watch() function