Loading
Russian Rebouças
This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.
Front End Developer
http://goo.gl/XwcMr6
História
Arquitetura
Scope
Diretivas
Bootstraping
Data-Binding
Dependency Injection
Services
Routes
Ajax
Miško Hevery
Adam Abrons
2009
Feedback
Aposto que consigo refazer tudo com meu novo Framework em apenas duas semanas! :)
I hereby declare AngularJS to be MVW framework - Model-View-Whatever. Where Whatever stands for "whatever works for you".
angular.module('HelloWorld', [])
angular.module('HelloWorld')
.controller('MyController', function($scope) {
$scope.message = "Hello World!";
})
<!DOCTYPE html>
<html ng-app="HelloWorld">
<head>
<title>Hello World</title>
<script src="path/to/angular.js"></script>
</head>
<body ng-controller="MyController">
<h1>{{message}}</h1>
</body>
</html>
<li ng-repeat="item in items">
{{item.name}}
</li>
<flex-calendar
options="calendarOptions"
events="calendarEvents">
</flex-calendar>
<form name="movie">
<input ng-model="title">
<input ng-model="director">
...
</form>
NG-APP
NG-CONTROLLER
NG-MODEL
NG-REPEAT
NG-CLICK
NG-BIND
NG-IF
NG-SWITCH
NG-CHANGE
NG-HIDE
NG-SHOW
NG-REPEAT
HTML
CSS
JS
DOM
CONTENT
LOADED
ng-app
directives
$watch
$watch
$watch
$watch
VIEW
DIGEST
CYCLE
events
update view
Event Loop
Angular Context
User: <input type="text" ng-model="user" />
Password: <input type="password" ng-model="pass" />
0 - USERNAME
1 - PASSWORD
=
.controller('MainCtrl', function($scope) {
$scope.foo = "Foo";
$scope.world = "World";
});
0 - WORLD
=
<P> Hello, {{ World }} </P>
.controller('MainCtrl', function($scope) {
$scope.people = [...];
});
(2 * 10) + 1
=
<ul>
<li ng-repeat="person in people">
{{person.name}} - {{person.age}}
</li>
</ul>
$watch 1
$watch 2
$watch 3
$watch 4
$watch 5
$watch 6
$watch 7
$watch 8
. . .
$watch list
Hey $watch, qual o seu valor?
É 9
Ok, ele mudou?
Não senhor!
Você, Qual o seu valor?
É 'foo'
Houve mudança?
É 'bar'
Bom! Nós temos um DOM a ser atualizado! :D
Nada acontece
<button ng-click="someFunction()">
<form>
<input type"email" ng-model="user.email">
<input type"password" ng-model="user.password">
<button ng-click="login(user)">
</form>
// PSEUDOCODE
$apply = function(fn) {
try {
fn();
} finally() {
$digest();
}
}
<input type="checkbox" ng-model="value"> Value
ThirdyPartyApi.foo(param, function(result) {
$scope.somVariable = result;
});
ThirdyPartyApi.foo(param, function(result) {
$scope.$apply(function() {
$scope.somVariable = result;
});
});
// PSEUDOCODE
$apply = function(fn) {
try {
fn();
} finally() {
$digest();
}
}
// Register a Module
angular.module('moduleName',[]);
// Locate a Module
angular.module('moduleName');
// Register a Module with Dependencies
angular.module('moduleName',['dependency']);
Create A
Create B
Inject A
Create C
Inject B
.controller('MyController', function($scope, $window) {
$scope.foo = 'bar';
$scope.height = $window.innerHeight;
});
.controller('MyController', ["$scope", "$window", function(scope, window) {
scope.foo = 'bar';
scope.height = window.innerHeight;
}]);
angular.module('myApp',[])
.controller('MyController', MyController);
MyController.$inject = ['$scope', '$window']
function MyController (scope, window) {
$scope.foo = 'bar';
$scope.height = $window.innerHeight;
}
angular.module('App', [])
.controller('MoviesController', function($scope, Movies) {
Movies.list()
.success(function(response){
$scope.moviesList = response.data;
})
.error(function(response) {
console.log(response);
})
})
angular.module('App')
.factory('Movies', function($http) {
return {
list: function() {
return $http.get('http://example.com/api/movies');
},
get: function(id) {
return $http.get('http://example.com/api/movies/'+id);
}
}
})
Create F
Create C
Inject F
angular.module('App', [])
.controller('MoviesController', function($scope, Movies) {
Movies.list()
.success(function(response){
$scope.moviesList = response.data;
})
.error(function(response) {
console.log(response);
})
})
angular.module('App')
.factory('Movies', function($http) {
this.list = function() {
return $http.get('http://example.com/api/movies');
}
this.get = function(id) {
return $http.get('http://example.com/api/movies/'+id);
}
})
Create S
Create C
Inject S
angular.module('App', [])
.provider('helloWorld', function() {
this.setName = function(name) {
this.name = name;
};
this.name = 'Default';
this.$get = function() {
var name = this.name;
return {
sayHello: function() {
return "Hello, " + name + "!"
}
}
};
});
myapplication/
View
homeController
home.html
Controller
myapplication/sobre
sobreController
sobre.html
myapplication/contato
contatoController
contato.html
$routeProvider
angular.module('myApp', ['ngRoute'])
.config(function($routeProvider) {
$routeProvider
// Rota para a pagina Inicial
.when('/', {
templateUrl : 'pages/home.html',
controller : 'HomeController'
})
// Rota para a pagina Sobre
.when('/sobre', {
templateUrl : 'pages/sobre.html',
controller : 'SobreController'
})
// Rota para a pagina Contato
.when('/contato', {
templateUrl : 'pages/contato.html',
controller : 'ContatoController'
});
});
ng-view
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>NgRoute</title>
<script src="path/to/angular.js"></script>
</head>
<body>
<div ng-view>
<!-- Aqui é onde as views serão renderizadas -->
</div>
</body>
</html>
$http
$http.get('http://exampel.com/api/movies')
$http.post('http://example.com/api/movie')
$http.put('http://example.com/api/movie/1')
$http.patch('http://example.com/api/movie/1')
$http.delete('http://example.com/api/movie/1')
$http
$http.get('http://example.com/api/movies')
.then(function(response){
$scope.movies = response.data;
})
.catch(function(response){
BeautifulAlert('Ops! Tivemos um probl...');
})