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