-
Notifications
You must be signed in to change notification settings - Fork 1
AngularJS: Get Started
Pritesh Patel edited this page Feb 6, 2019
·
12 revisions
- builtwith.angularjs.org - sample apps
- plnkr.co - web-based IDE
- add <script> tag pointing to angular.js and an ng-app attribute in HTML
- Browser developer tools
- console tab
- sources tab (Ctrl + O) and set breakpoints in script files for debugging
- JavaScript as a functional language
- Functions as abstractions (passing functions as parameters)
- Functions to build modules
- The IIFE (Immediatley-Invoked Function Expression) (avoids declaration of global variables)
- A controller is responsible for building a model
- 4 basic facts:
- ng-controller directive
- controller is a function that Angular invokes
- controller takes a $scope parameter
- attach model to $scope
- Controller focuses on scope, View focuses on HTML with directives (separation of concerns)
- multiple controllers, complex objects, nested controllers
- $http service to make http calls - GET, POST, PUT, DELETE...
- ask for $http inside a controller
- $http.get() always returns a promise. call then() method on the returned promise
- accessing the GitHub API
- e.g. $http.get("https://api.github.com/users/p-patel").then(...);
- pass second function parameter to .then() call to handle api call error condition
- Controllers usually live in modules (avoid global namespace)
var app = angular.module("moduleName", []);- register controllers in the module
app.controller("MainController", MainController) - tell Angular to use your module with ng-app
<body ng-app="moduleName">
- code example
- Data binding directives - {{message}} - moves model data to view
- use attribute
ng-model="username"to read data from view inputs into scope model
- add attribute
ng-click="search()"to run function when button is clicked - alternatively use
ng-submit="search()"on form element
- `
- use filters to format view data
- basic format:
expression |filterName:parameter - e.g. currency, date, filter, json, limitTo, lowercase/uppercase, number, orderBy
- bind sort option with view input
-
ng-show="user"- if this expression is truthy, element is displayed - ng-hide directive works in a similar way
- bring in html from another source (to breakdown html into components and reuse markup components)
<div ng-include="'userdetails.html'" ng-show="user"></div>
- over 50 directives in the core library inc mouse-related and key-press directives
- can also create custom directives
- View consumes the model, directives are an intermediary
- Controllers, models and directives can use services
- $timeout / $interval Angular services
- Controller - relates to setting up the model
- Use services from controllers, directives and other services
- $log service can be used to add logging (to client console and server-side)
- UI services can be used to make changes to the View without touching the UI-related object directly from the Model
- e.g $window, $browser, $location, $animate, $anchorScroll
- motives for creating custom services: create reusable logic, create shared data (a service exists as a single instance), manage complexity
- include service's new js file in index.html (after the script that defines the app module that the custom service will be used in)
- multiple methods to register a service with Angular e.g.
var module = angular.module("githubViewer");
module.factory("github", github);
- Routing based on URL, routing engine captures request, routing rules render a response
- Depends on module: angular-route.js, configure rules into $routeProvider e.g.
$routeProvider.when("/main", {
templateUrl : "mainhtml",
controller: "MainController"
})
- default.html becomes a layout view
- ng-view directive specifies where within the layout view to insert the template for the route
- add angular-route.js
- add app.js - define the app module and configure routes:
app.config(function($routeProvider){
$routeProvider
.when("/main"), {
templateUrl: "main.html,
controller: "MainController"
})
.otherwise({redirectTo:"/main"});
});
- create route, create template, create controller, have search navigate to new route
-
.when("/user/:username")where username is a querystring parameter -
$routeParams.username- service provides access to querystring parameters -
$location.path("/user/" + username);changes the client fragment to#/user/<someuser> - `Back to main - another way to route
- add a page to display details, open issues and collaborators (with their gravitars) of a given user's repository
- route: /repo/:username/:reponame, template: repo.html, controller: RepoController, update github service with new functionality