Angular
Seccions d'aquesta pàgina 8
01General#
- Angular (AngularJS
>= 2.0)
- Languages
- JavaScript
- TypeScript
- Dart
- Basics
- Overview
- Learning path
- Architecture (Basics)
- Quick start
- Tutorial
- Displaying
data (Basics)
- User input (Basics)
- Forms (Basics)
- Dependency injection (Basics)
- Template syntax (Basics)
- Architecture
- Displaying
data
- User input
- Forms
- Dependency injection
- Template syntax
- Angular cheat sheet
- Style guide
- Glossary
- Buiding blocks
-
description
properties
HTML template
Modules boxes for:
Components class to manage templates
Services application logic
types:
Angular modules (@NgModule)
- declarations:
the view classes that belong to this module
- exports:
the subset of declarations that should be
visible and usable in the component templates of
other modules.
- imports:
other modules whose exported classes are needed
by component templates declared in this module
- providers:
creators of services that this module
contributes to the global collection of
services; they become accessible in all parts of
the app
- bootstrap:
the main application view, called the root
component, that hosts all other app views. Only
the root module should set this bootstrap
property.
app/app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
imports: [ BrowserModule ],
providers: [ Logger ],
declarations: [ AppComponent ],
exports: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
app/main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
- declarations:
the view classes that belong to this module
-
name
app
one root module
AppModule
several featured modules
Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
-
view classes
components
directives
pipes
Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
- Modules
- Components
- Templates
- Metadata
- Data binding
-
direction
example
DOM
<-
component
{{value}}
interpolation
<li>{{hero.name}}</li>
<-
[property] = "value"
property binding
<hero-detail [hero]="selectedHero"></hero-detail>
->
(event) = "handler"
event binding
<li (click)="selectHero(hero)"></li>
<->
[(ng-model)] = "property"
two-way data binding
<input [(ngModel)]="hero.name">
Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
- Directives
-
kind
description
examples
component a decorator function that takes a metadata object as argument
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})@Component({
selector: 'hero-list',
templateUrl: 'app/hero-list.component.html',
directives: [HeroDetailComponent],
providers: [HeroService]
})
structural alter layout by adding, removing, and replacing elements in DOM
attribute alter the appearance or behavior of an existing element
...<input [(ngModel)]="hero.name">
Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
- Services
- Dependency injection
- "An injector maintains a container of service instances
that it has previously created."
- provider (registration)
- at general level: app/main.ts (bootstrap)
bootstrap(AppComponent, [BackendService, HeroService, Logger]);- at component level (@Component metadata):
app/hero-list.component.ts (providers)
@Component({
...
providers: [HeroService]
})
- usage
- app/hero-list.component.ts
constructor(private my_service: HeroService) { }- Other
- Animations
- Bootstrap
- Change detection
- Component router
- Events
- Forms
- HTTP
client
- Lifecycle hooks
- Pipes
- Router
- Testing
- Quick start
-
TypeScript JavaScript
Prerequisites
Step 1: Create and configure the project
Create the project folder mkdir angular2-quickstart
cd angular2-quickstart
Add package definition and configuration files - package.json
- tsconfig.json
- typings.json
- systemjs.config.js
- package.json
Install packages npm install
Step 2: Our first Angular component
Create an app subfolder
mkdir app
Create the component file
app/app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }
app/app.component.js
(function(app) {
app.AppComponent =
ng.core.Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
.Class({
constructor: function() {}
});
})(window.app || (window.app = {}));
Step 3: Add main
app/main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
bootstrap(AppComponent);
app/main.js
(function(app) {
document.addEventListener('DOMContentLoaded', function() {
ng.platformBrowserDynamic.bootstrap(app.AppComponent);
});
})(window.app || (window.app = {}));
Step 4: Add index.html
Step 5: Build and run the app
ng build [-prod]
ng serve
ng serve --host 0.0.0.0
Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
- Tutorial
- JavaScript
- TypeScript
- AngularJS (Google) (wp)
- Instal·lació / Installation
- Install Node.js
- AngularJS: Developer guide
- Djangular (Django and AngularJS)
- Angular Modules
- Angular Style Guide
- 10
Reasons Why You Should Use AngularJS
- Django Rest Framework
- Info
- Tutorials
- PhoneCat Tutorial App
- Install Node.js
- Install needed server packages for project
npm install- get index of packages to be installed from file: your_project/package.json
- install them in node_modules directory
- Install needed client packages for project
- Install Bower (system-wide)
sudo npm install -g bowerbower install- get index of packages to be installed from file: your_project/bower.json
- install them in your_project/app/bower_components/
directory
- AngularJS
Fundamentals In 60-ish Minutes (video)
- A
Step-by-Step Tutorial for Your First AngularJS App (part
1)
git clone --depth=1 https://github.com/angular/angular-seed.git f1feeder-part1cd f1feeder-part1npm installnpm start
- Your First AngularJS App Tutorial Part 2: Tools for Scaffolding, Building, and Testing
- Karma, Jasmine, Grunt, Bower, Yeoman
- steps
mkdir f1feeder-part2cd f1feeder-part2yo angulargrunt serve- Exemples / Examples
- UI
- AngularUI
- Bootstrap
- Upload
- Mobile
- The Definitive Guide to Angular on Mobile
- Apache Cordova
- Javascript tools
02Bastida / Scaffolding#
- AngularJS
best practices: directory structure
- Yo
- generators
- examples
- usage
mkdir my_projectcd my_projectyo angulargrunt serve
- troubleshooting
- yo creates files in wrong dir
- search for file .yo-rc.json and remove it
- Estructura / Structure
bower.json.bowerrc.editorconfig.gitattributes
.gitignoreGruntfile.js.jscsrc.jshintrcpackage.jsonREADME.md.yo-rc.json
app/index.htmlrobots.txt
images/yeoman.png
scripts/app.jscontrollers/about.jsmain.js
styles/main.css
views/about.htmlmain.htmltest/(bower_components/)(node_modules/)
- angular-seed
- Instal·lació / Installation
git clone --depth=1 https://github.com/angular/angular-seed.git my_project
cd my_projectnpm install- will install (according to bower.json):
node_modulesapp/bower_componentsnpm start- http://localhost:8000/app/index.html
03Editors#
- Eclipse
plugins
- Angular2 Eclipse
- Dependencies
npm install -g angular-cli
- AngularJS Eclipse
- Based on Tern
- Problemes / Problems
- 100% CPU
- Tern
performance
- Solució / Solution
rm my_project/.settingsrm my_project/.tern-project- check Script Path
- editorconfig-eclipse
04Debug#
- Batarang
(plugin for Chrome)
05Django Rest Framework#
- Autenticació / Authentication
- django-rest-auth
- structure generated by Yo
- Getting Started with Django Rest Framework and AngularJS
- django-api-rest-and-angular
- Install
- Python requirements
virtualenv envsource env/bin/activate
pip install -r requirements.txt
python setup.py develop- Note: it works with django==1.8. It does not work with
django==1.9
- install Bower and Grunt
- install assets
npm install
bower install- compile assets
grunt- setup the database
make create_database; make make_fixtures- run the server
./manage.py runserver- Problemes / Problems
06Video#
- What I learnt about HTML5 video and AngularJS
- AngularJS video source directive
- Problemes / Problems
- ngSrc doesn't work properly with HTML5 Video Source tag #1352
- Solucions / Solutions
- HTML5 Video is not working with AngularJS ng-src tag
- videojs + hls
- Si video es troba dins de ng-view, cal afegir la directiva vjs-video. Si no, no fa cas de class="video-js vjs-default-skin" i els controls són del video HTML5 per defecte.
- mp4
- per a poder fer servir {{}}, cal posar html5vfix i vsrc directament a l'etiqueta video. Si la posem a l'etiqueta source, no funciona.
- hls
- no funciona si posem una src fixa dins de l'etiqueta video
- sí que funciona si posem una src fixa dins de l'etiqueta source
- no funciona amb html5vfix
-
directive
-
vjs-video
static
mp4
<video class="video-js vjs-default-skin">
<source src="...mp4">
ok (però no fa autostart)
ok (però no fa autostart) <ng-view>
<video class="video-js vjs-default-skin">
<source src="...mp4">html5 video bàsic (fa autostart)
ok (fa autostart)
hls
<video class="video-js vjs-default-skin">
<source src="...m3u8">
ok (fa autostart) ok (fa autostart) <ng-view>
<video class="video-js vjs-default-skin">
<source src="...m3u8">ok (fa autostart) ok (fa autostart) dynamic
mp4
<video class="video-js vjs-default-skin">
<source vsrc="...mp4" html5vfix>ok (fa autostart) x
video src="undefined"
source vsrc="...mp4" src="...mp4"<video class="video-js vjs-default-skin" vsrc="...mp4" html5vfix>
ok (fa autostart) ok (fa autostart) <ng-view>
<video class="video-js vjs-default-skin">
<source vsrc="...mp4" html5vfix>html5 video bàsic (fa autostart)
(però si algun altre al pare ha posat vjs-video, va bé)
si té un id, la primera vegada ho fa bé (però sense autostart), però si es recarrega ja torna a html5 video bàsic
x
video src="undefined"
source vsrc="...mp4" src="...mp4"<ng-view>
<video class="video-js vjs-default-skin" vsrc="...mp4" html5vfix>
ok (fa autostart) ok (fa autostart) hls
<video class="video-js vjs-default-skin">
<source vsrc="...m3u8" html5vfix>ok (fa autostart) negre (no fa start)
video
source vsrc="...m3u8" src="...m3u8"
<video class="video-js vjs-default-skin" vsrc="...m3u8" html5vfix> x
video vsrc="...m3u8" src="...m3u8"
x
video vsrc="...m3u8" src="...m3u8"<ng-view>
<video class="video-js vjs-default-skin">
<source vsrc="...m3u8" html5vfix>ok (fa autostart) negre (no fa start)
video
source vsrc="...m3u8" src="...m3u8"<ng-view>
<video class="video-js vjs-default-skin" vsrc="...m3u8" html5vfix>x
video vsrc="...m3u8" src="...m3u8"x
video vsrc="...m3u8" src="...m3u8"Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
- Reproductors de vídeo HTML5 / HTML5 Video players
- Videogular
- HLS
- bower-videogular-hls
- needs Dailymotion hls.js
- ngVideo
- ngMedia
- Video.js
- Alternative Setup for Dynamically Loaded HTML
- AngularJS controller example
- VideoJS with AngularJS #808
- directive example
- angular single page app, videoJS ready function only fires on page load
- Directive
on same element as ng-repeat, need ng-repeat to run first
and use its value
- AngularJs and VideoJs problem: Video only loaded on hard refresh, not on switching page
- Solution
- directives.js
angular.module('myProject.directives', [])
.directive('html5vfix', function() {
return {
restrict: 'A',
link: function(scope, element, attr) {
attr.$set('src', attr.vsrc);
attr.$set('type', attr.vtype);
}
}
})
.directive('myvideojs', function () {
var linker = function (scope, element, attrs){
var setup = {
'techOrder' : ['html5', 'flash'],
'controls' : attrs.controls=="" || false,
'loop' : attrs.loop || false,
'preload' : 'auto',
'autoplay' : attrs.autoplay || false,
'height' : attrs.height || 180,
'width' : attrs.width || 320
};
element.attr('id', attrs.id);
var player = videojs(attrs.id, setup, function(){
});
scope.$on('$destroy', function () {
player.dispose();
});
}
return {
restrict : 'A',
link : linker
};
});
- index.html
<!DOCTYPE html>
<html>
<head>
<title>My Title</title>
<link href="styles/app.css" rel="stylesheet" type="text/css">
<!-- videojs css -->
<link href="http://vjs.zencdn.net/5.6.0/video-js.css" rel="stylesheet">
</head>
<body ng-app="myProjectApp">
<h1>My Project</h1>
<ng-view></ng-view>
<!-- angular.js -->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<!-- video.js -->
<script src="http://vjs.zencdn.net/5.6.0/video.js"></script>
<!-- hls for video.js -->
<script src="scripts/videojs.hls.js"></script>
<!-- app -->
<script src="scripts/app.js"></script>
<script src="scripts/services/services.js"></script>
<script src="scripts/controllers/controllers.js"></script>
<script src="scripts/directives/directives.js"></script>
</body>
</html>
- my_partial.html
<li ng-repeat="my_video in my_videos">
<video myvideojs id="{{my_video.id}}" class="video-js vjs-default-skin" preload="auto" autoplay="true" controls>
<source vsrc="{{myvideo.url}}" type="{{myvideo.mime_type}}" html5vfix/>
</video>
</li>
- vjs-video
07Desenvolupament / Development#
- Examples
- Add some control
-
index.html
todo.js
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script src="todo.js"></script>
ng-app="todoApp"
angular.module('todoApp', [])
.controller('TodoListController', function() {
var todoList = this;
todoList.todos = [
{text:'learn angular', done:true},
{text:'build an angular app', done:false}];
todoList.addTodo = function() {
todoList.todos.push({text:todoList.todoText, done:false});
todoList.todoText = '';
};
todoList.remaining = function() {
var count = 0;
angular.forEach(todoList.todos, function(todo) {
count += todo.done ? 0 : 1;
});
return count;
};
todoList.archive = function() {
var oldTodos = todoList.todos;
todoList.todos = [];
angular.forEach(oldTodos, function(todo) {
if (!todo.done) todoList.todos.push(todo);
});
};
});
ng-controller="TodoListController as todoList"
<li ng-repeat="todo in todoList.todos">
<input type="checkbox" ng-model="todo.done">
<form ng-submit="todoList.addTodo()">
Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
08i18#
- Angular
- Internationalization (i18n)
- Angular2 i18n at this point?
- i18n-sample
- Angular
i18n update from ng-conf 2015
- AngularJS 1.0
http://www.francescpinyol.cat/angular.html
Primera versió: / First version: 10.II.2016
Darrera modificació: 20 d'agost de 2020 / Last update: 20th August
2020