AngularJS Flashcards
1
Q
dependency injection
A
- when pass a dependency (function or object) to a function to use, instead of allowing the function to look for it itself, or to define it inside its body
2
Q
anchor tags
A
- mechanism in html for moving within document using hyperlinks
Steps
- create a tag with href
- <‘a href=”#section1”>Go to Section 1<‘/a>
- Create a tag with corresponding id
- <‘a id=”section1”>This is Section 1<‘/a>
3
Q
service
AngularJS
A
- basically an Angular function or object
- almost always a singleton ($scope is an exception)
- custom services can be used implement app-wide functionality
- or to pass data between controllers
- some services are built in
- $scope
- $log
- $timeout
- 3 ways to register (create) custom services
- app.service() function
- app.factory() function
- app.provider() function
4
Q
model
AngularJS
A
- any data that lives within the scope of an angular controller
- $scope variable is used to store data that exists in the model
- $scope (and therefore model) is unique for every controller
5
Q
view
AngularJS
A
- view is just some html that is linked to a controller
- usually two-way bindings
2 contexts
- portion of a regular html document (index.html)
- linked to controller via the ngController directive
- standalone html file
- use the ngView directive to insert into page
- linked to controller via $routeProvider
6
Q
controller
AngularJS
A
- component of an AngularJS app that operates on the model (data)
- each controller has its own scope (own model)
- controllers are linked to views
7
Q
routing
AngularJS
A
- used to display different html to user based on active url hash
Steps
- Add ngRoute module in script tag
- Inject ngRoute dependency into main app module
- Use myApp.config, and inject $routeProvider dependency
- Each url hash should be linked to:
- tempalteUrl
- controller
8
Q
directives
A
- html-side commands
- analagous to template tags and filters in Django or liquid in Shopify {% %}
2 flavors
- Built-in directives
- ng-model
- ng-view
- ng-controller
- Custom directives
9
Q
ngController
A
- built in AngularJS directive
- essentially turns a portion of html into a view (linked to a specific controller)
10
Q
ngView
A
- built in AngularJS directive
- designates a location in page to imports html from a standalone document
- used in conjuction with routing
- consults the $routeProvider to determine which template to import (based on the current url hash)
11
Q
ngTransclude
A
- built-in Angular directive
- allows you to incorporate any html located in your custom directive tag into the generated view
Steps
- Add some text inside the tag of your custom directive (the spot where directive was added)
- Add ‘transclude’ to directive object, set to true
- Add ngTransclude tag to the inside of the directive’s html template
12
Q
custom directives
A
- custom directives are essentially snippets of reusable html they can be added to an html page
- can sit inside or outside of a view
Steps
- Create directive on app
- myApp.directive(‘directiveName’, function() {} )
- Create an object (directive definition object) inside function that takes several options
- Return directive object
13
Q
directive definition object
A
- object you define when creating a directive
- has several predefined options:
- templateUrl ⇒ relative location of the html use to generate the directive’s ouptut
- scope object ⇒ used to create isolate scope
- compile function ⇒ allows you to edit directive html before generation
- link function ⇒ allows you to edit each individual instance of directive DOM
- controller constructor ⇒ allows you to share data between directives
- transclude ⇒ boolean indicating whether ngTransclude is usee
14
Q
isolating the scope
A
- process of separating scope (model) of custom directive from scope of its enclosing view
- defines a unique scope for every instance of custom directive
Steps
- Add scope property to directive object
- If you “poked holes” in scope, list variables you wish to access
15
Q
“poking holes” in the scope
A
- allow directive with isolated scope to access variables in parent scope (enclosing view’s model)
Steps
- In site of “instantiation”, request access to a variable located in the parent scope by…
- Adding custom attributes to tag of custom directive
- Where the key is the name of the variable in the isolate scope and the value is the name of variale you want
- On directive side, add key/val pairs to the scope object for each variable you requested…
- Where keys are same as the keys of custom attributes (in camel case)
- And values are symbols used to indicate type of data being passed
- ’@’ → text (one-way binding)
- ’>’ → object with one-way binding
- ’=’ → object with two-way binding
- ‘&’ → function
- On the template side, access variables using names as defined in scope object
16
Q
“compilation”
AngularJS
A
- does not relate to compiling and linking in traditional CS
- process of turning a custom directive into a DOM element
- four functions are invoked (in this order) when directive is “instatiated”
- compile
- controller constructor
- pre-linking
- post-linking
17
Q
compile
AngularJS
A
- function that executes during the “compilation” of a custom directive
- during compile phase, scope doesn’t exist
- function allows you to change raw template of custom directive (which will effect all instances)
Syntax
- compile: function(elem, attrs) { }
Steps
- Add compile as key in directive definition object
- Perform any changes using arguments
- Optionally, define and return linking functions
18
Q
controller (directive)
[AngularJS]
A
- function that executes during “compilation” of custom directive
- function can be used, like ‘link’, to tweak instance of directive
- but used primarily to pass data to other directives
- in this sense, more like a shared scope object than an actual controller
Syntax
- controller = function($scope) { // this bindings }
Steps
- Attach methods and properties to controller object using this binding
- this always refers to the controller itself in this situation
- Elsewhere, another directive uses ‘require’ to access this directive’s controller object
- Then it read its properites like an normal object
19
Q
linking
AngularJS
A
- two functions invoked during “compilation” of custom directive
- allows you to update the instance of custom directive
- or attach events to generated DOM element
2 functions:
- pre-linking
- Code runs, and then nested directives are compiled and linked
- Almost never done, because additional changes might be made by nested directives
- post-linking
- after all nested directives are compiled and linked, code runs
- this is what “linking” really is
2 approaches:
- Defined within compile: method
- Defined separated in link: method
20
Q
defining linking functions in
link: property
A
- use link: property defined in directive definition object
2 choices
- Make link: property is an object
- Object has methods pre: and post:
- You define pre-linking and and post-linking in that object
— or —
- Make link: property is a method
- That function is treated as the post-linking function
21
Q
defining linking functions in compile property
A
- defining linking function in compile property is optional
- Not preferred
2 choices
- Create an object with methods pre: and post:
- You define pre-linking and and post-linking in that object
- Return that object
— or —
- Return a function
- That function is treated as the post-linking function