index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularJs QuickStart</title>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en" ng-app>
<head>
<meta charset="UTF-8">
<title>AngularJs QuickStart</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js"></script>
</head>
<body>
</body>
</html>
<input type="text" ng-model="name" placeholder="Insert your name!"/>
<div>Hi {{ name }}!</div>
<input type="text" ng-model="name" placeholder="Insert your name!"/>
<div>Hi {{ name }}!</div>
<div>How are you {{ name }}?</div>
<input type="number" ng-model="a"/>
<h4>+</h4>
<input type="number" ng-model="b"/>
<h3>Result is {{a+b}}!</h3>
<button ng-click="value = 3" ng-init="value = 1">Set Value to 3</button>
<h3>Value is: {{value}}</h3>
<button ng-click="value = value + 1" ng-init="value = 1">Increase Value</button>
<h3>Value is: {{value}}</h3>
<button ng-click="value = value + 1" ng-init="value = 1">Increase Value</button>
<h4>Value is: {{value}}</h4>
<h3 ng-hide="value > 3">I will be hidden if value is greater than 3</h3>
<h3 ng-show="value > 3">I will be visible if value is greater than 3</h3>
<input type="text" ng-model="name"/>
<button ng-click="array.push(name)" ng-init="array = []">Insert Name</button>
<h3 style="margin-top: 10px">Value is: {{array}}</h3>
<input type="text" ng-model="name"/>
<button ng-click="array.push(name)" ng-init="array = []">Insert Name</button>
<h2 ng-repeat="name in array">{{name}}</h2>
<input type="text" ng-model="name"/>
<button ng-click="array.push(name)" ng-init="array = [...]">Insert Name</button>
<input type="text" ng-model="query"/>
<h2 ng-repeat="name in array | filter:query">{{name}}</h2>
Name | Age |
---|---|
{{person.name}} | {{person.age}} |
<!-- Insert -->
<!-- Participant = {name: 'Matteo', age: '29'} -->
<input type="text" ng-model="participant.name"/>
<input type="number" ng-model="participant.age">
<button ng-click="list.push({name: participant.name, age: participant.age})"
ng-init="list = []">Insert</button>
<!-- Filter -->
Name <input type="text" ng-model="query.name">
Age <input type="text" ng-model="query.age">
<!-- Repeat -->
<div ng-repeat="person in list | filter:query">
<span>{{person.name}}</span>
<span>{{person.age}}</span>
I prodotti devono essere salvati in un array products
Ognuno dei prodotti deve avere queste caratteristiche:
{category: String, name: String, quantity: Number}
Altri binding utili: ng-submit
ng-options (hard)
ng-model
ng-click
ng-repeat
ng-submit
console.log(myVar);
angular.module('groceryStore',[])
.controller('listCtrl', function($scope){
$scope.myFunction = function(a){
console.log(a);
// do stuff;
return b;
}
});
main.js
angular.module('groceryStore',[])
.controller('listCtrl', function($scope){
// my code
});
<section ng-controller="listCtrl">
</section>
Controller
?$scope
?controller
to Htmlcontroller
to Html
<section ng-controller="listCtrl" class="row">
<article class="col-sm-9">
// Form and List Html
</article>
<article ng-controller="cartCtrl" class="col-sm-3">
// Cart Html
</article>
</section>
$scope
angular.module('groceryStore',[])
.controller('listCtrl', function($scope, $http){
$scope.categories = ['Fruits', 'Vegetables'];
$scope.products = [
{
category : "Fruits",
name : "Apple",
quantity : "12"
},
{
...
}
];
});
$scope
<div class="well" ng-repeat="product in products | filter:query">
<div class="row">
<div class="col-sm-3">{{product.category}}</div>
<div class="col-sm-3">{{product.name}}</div>
<div class="col-sm-3">{{product.quantity}}</div>
</div>
</div>
$scope
angular.module('groceryStore',[])
.controller('listCtrl', function($scope){
$scope.addProduct = function(){
$scope.products.push($scope.newProduct);
$scope.newProduct = null;
};
$scope.addToCart = function(product){
$scope.cart.push(product);
};
});
$scope
<form ng-submit="addProduct()"> ... </form>
<div class="well" ng-repeat="product in products | filter:query">
<div class="row">
<div class="col-sm-3">{{product.category}}</div>
<div class="col-sm-3">{{product.name}}</div>
<div class="col-sm-3">{{product.quantity}}</div>
<div class="col-sm-3">
<a href="" ng-click="addToCart(product)" class="btn btn-primary">
Buy
</a>
</div>
</div>
</div>
listCtrl
.push
)
angular.module('groceryStore',[])
.controller('parentCtrl', function($scope){
$scope.parentMethod = function(){/*...*/};
$scope.parentValue = "Matteo";
})
.controller('childCtrl', function($scope){
$scope.parentMethod(); //it'll work
// and I can read parentValue
});
<div ng-controller="parentCtrl">
<!-- Some binding -->
{{parentValue}} <!-- Matteo -->
<div ng-controller="childCtrl">
<!-- Some other binding -->
{{parentValue}} <!-- Matteo -->
<a ng-click="parentMethod()"> <!-- it'll work -->
</div>
</div>
angular.module('groceryStore',[])
.controller('parentCtrl',
function($scope){
$scope.parentValue = "Matteo";
})
.controller('childCtrl',
function($scope){
$scope.parentValue = "Giovanni";
});
<div ng-controller="parentCtrl">
<!-- Some binding -->
{{parentValue}} <!-- Matteo -->
<div ng-controller="childCtrl">
<!-- Some other binding -->
{{parentValue}} <!-- Giovanni -->
</div>
</div>
cartCtrl
$scope.cart
cartCtrl
http
service
var req = {
method: 'POST',
url: 'http://example.com',
data: { test: 'test' }
};
$http(req)
.success(function(res){...})
.error(function(err){...});
$http.get('http://example.com')
.success(function(res){...})
.error(function(err){...});
var data = {firstName: 'Matteo', occupation: 'Frontend Developer'};
$http.post('http://example.com', data)
.success(function(res){...})
.error(function(err){...});
.success
.success(function(res, status, headers, config){
// executed for 200 and 300 statuses
})
.error
.error(function(res, status, headers, config){
// executed for 400 and 500 statuses
})
http
return a promise
var data = {firstName: 'Matteo', occupation: 'Frontend Developer'};
$http.post('http://example.com', data)
.then(function(res){
// this is the success case
})
.catch(function(err){
// this is the error case
});
angular.module('groceryStore',[])
.controller('listCtrl', function($scope, $http){
// Retrieve data form the backend
$http.get('../mocks/list.json')
.success(function(list){
$scope.products = list;
})
.error(function(err){
$scope.error = err.data.message;
});
});
$http
from mocks/list.json
/users [GET]
Query the list of users/users/1 [GET]
Get a single user/users [POST]
Create a user/users/1 [POST]
Update a user/users/1 [DELETE]
Delete a user$resource
var User = $resource('/user/:userId', {userId:'@id'});
/users [GET]
Query the list of users
var users = User.query().then(successHandler, errorHandler);
/users/1 [GET]
Get a single user
var user = User.get({userId: 1}).then(successHandler, errorHandler);
/users [POST]
Create a user
var newUser = new User({name: 'Matteo'});
newUser.$save().then(successHandler, errorHandler);
/users/1 [POST]
Update a user
user.name = 'Giovanni';
user.$save();
/users/1 [DELETE]
Delete a user
user.$remove();
.service
.directive
.filter
.controller
.service
.directive
.filter
.run
.config
angular.module('groceryStore',[])
.service('myService', function(){
// store some data
this.myData = 'data';
// define a method
this.getData = function(){
return this.myData;
};
});
angular.module('groceryStore',[])
.controller('myCtrl', function(myService){
$scope.data = myService.getData();
});
$http
request?
$http.get('../mocks/list.json');
.success(function(list){
$scope.products = list;
})
.error(function(err){
throw err;
});
.service
angular.module('groceryStore',[])
.service('listService', function($http){
this.getList = function(){
return $http.get('../mocks/list.json');
}
});
.controller
angular.module('groceryStore',[])
.controller('listCtrl', function($scope, listService){
// Retrieve data form the backend
listService.getList()
.success(function(list){
$scope.products = list;
})
.error(function(err){
throw err; // or better notify the user
});
});
$http
call in a listService
angular.module('groceryStore',[])
.service('listService', function($http){
// code
});
angular.module('groceryStore',[])
.service('listService', ['$http', function($http){
// code
}]);
listCtrl
and cartCtrl
cartService
to handle the cart with:this.cart
to store the datathis.add(product)
to add a productthis.remove(id)
to remove a product.service
definitionhttp://localhost:300/#
http://localhost:300/#/about
ngRoute
angular.module('groceryStore',[])
angular.module('groceryStore',['ngRoute'])
<script src="vendor/angular/angular.min.js"></script>
<script src="vendor/angular-route/angular-route.min.js"></script>
<script src="js/main.js"></script>
angular.module('groceryStore',['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'views/list.html',
controller: 'listCtrl'
}).
when('/about', {
templateUrl: 'views/about.html',
controller: 'aboutCtrl'
}).
otherwise({
redirectTo: '/'
});
}]);
angular.module('groceryStore',['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/myRoute/:id', {
// ....
})
.otherwise({
redirectTo: '/'
});
}])
.controller('myCtrl', function($scope, $routeParams){
console.log($routeParams.id);
});
When visiting #/myRoute/12
will log 12
When visiting #/myRoute
will not match the route
angular.module('groceryStore',['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/myRoute/:name?', {
// ....
})
.otherwise({
redirectTo: '/'
});
}])
.controller('myCtrl', function($scope, $routeParams){
console.log($routeParams.name);
});
When visiting #/myRoute/matteo
will log matteo
When visiting #/myRoute
will log undefined
angular.module('groceryStore',['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/myRoute', {
// ....
})
.otherwise({
redirectTo: '/'
});
}])
.controller('myCtrl', function($scope, $location){
console.log($location.search());
});
When visiting #/myRoute?name=matteo&age=29
will log {name: matteo, age: 29}
When visiting #/myRoute
will log undefined
<input type="text" ng-model="name" placeholder="Insert your name!"/>
<div>Hi {{ name }}!</div>
ng-include
<ng-include src="'path/to/template.html'"></ng-include>
html
templateng-view
directive
<div ng-view></div>
ng-view
load the template inside the provided container and bind the specified controller
when('/about', {
templateUrl: 'views/about.html',
controller: 'aboutCtrl'
}).
/
e /about
listCtrl
in un templateabout
├── app
│ ├── bower_components
│ ├── images
│ ├── scripts
│ │ ├── controllers
│ │ ├── directives
│ │ ├── services
│ │ └── app.js
│ ├── styles
│ ├── views
│ └── index.html
├── node_modules
└── test
├── bower_components
├── docs
├── e2e
├── gulp
├── node_modules
└── src
├── app
│ ├── modules
│ │ └── myModule
│ │ ├── directives
│ │ ├── service
│ │ ├── views
│ │ └── myModule.js
│ └── main.js
└── index.html