2. Que es AngularJS ?
AngularJS es un framework javascript opensource
desarrollado por ingenieros de Google.
Un framework para crear aplicaciones web del lado del
cliente, ejecutándose con el conocido single-page
applications (aplicación de una sóla página) .
Extiende el tradicional HTML con etiquetas propias
(directivas)
Basado en MVC (Modelo-Vista-Controlador).
4. Acerca del funcionamiento
1. El browser carga el html y parsea este
dentro del DOM.
2. El browser carga la libreria de
angular.js
3. Angular espera por el evento
DOMContentLoaded.
4. Angular busca la directiva ng-app, la
cual define el alcance de la aplicación.
5. El módulo especificado en ng-app (si
hay alguno) es usado para configurar
el $injector.
6. El $injector se utiliza para crear el
servicio $compile, así como el
$rootScope.
7. El servicio $compile es usado para
compilar el DOM y linkearlo con el
$rootScope.
5. Acerca del funcionamiento
Compiler: recorre el DOM y recoger todas las
directivas. El resultado es una función linkeada.
Link: combinar las directivas con un Scope y
produce un live view. Cualquier cambio en el
modelo del Scope se reflejan en la vista, y
cualquier interacción del usuario con la vista se
reflejan en el modelo del Scope. Esto produce Two
Way Data Binding
8. Directives
Extienden HTML para estructurar la aplicación
● Declarativo
● Usa la información que tiene en el Scope
● Crea el DOM al vuelo.
<div>
<div ng-repeat=”user in users”>
<h3>{{user.name}}</h3>
<h3>{{user.descripcion}}</h3>
</div>
</div>
Este itera en una colección en el Scope y crea el DOM.
11. Custom Filters
<!doctype html>
<html ng-app="MyReverseModule">
<head>
<script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="Ctrl">
<input ng-model="greeting" type="greeting"><br>
No filter: {{greeting}}<br>
Reverse: {{greeting|reverse}}<br>
Reverse + uppercase: {{greeting|reverse:true}}<br>
</div>
</body>
</html>
angular.module('MyReverseModule', []).
filter('reverse', function() {
return function(input, uppercase) {
var out = "";
for (var i = 0; i < input.length; i++) {
out = input.charAt(i) + out;
}
// conditional based on optional argument
if (uppercase) {
out = out.toUpperCase();
}
return out;
}
});
function Ctrl($scope) {
$scope.greeting = 'hello';
}
index.html script.js
12. Services
function myController($loc, $log) {
this.firstMethod = function() {
// use $location service
$loc.setHash();
};
this.secondMethod = function() {
// use $log service
$log.info('...');
};
}
// which services to inject ?
myController.$inject = ['$location', '$log'];
13. $http Service
$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
14. $http Service
$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Metodos
● $http.get
● $http.head
● $http.post
● $http.put
● $http.delete
● $http.jsonp