Professional Documents
Culture Documents
Angular Js
Angular Js
Angular Js
Table of Contents
1. introduction
i. Filters and a New Directive
2. Flatlander's Gem Store
i. RAMP UP
ii. Creating a Store Module
iii. Index HTML Setup
iv. Built-in Directives
v. Filters and a New Directive
vi. Tabs Inside Out
vii. Forms and Models
viii. Accepting Submissions
ix. Form Validations 101
x. Directives
3. First Chapter
i. json
ii. custom filter
iii. select ng-options
2
AngularJS
My Awesome Book
This file file serves as your book's preface, a great place to describe your book's content and ideas.
introduction 3
AngularJS
RAMP UP
HTML javascript
<body ng-controller="StoreController">
</body>
<script>
function StoreController(){
alert('Hello,World');
}
</script>
Getting Started
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
</head>
<body>
<script type="text/javascript" src="angular.min.js"></script>
</body>
</html>
Modules
Where we write pieces of our Angular application.
Makes our code more maintainable, testable, and readable.
Where we define dependencies for our app.
RAMP UP 4
AngularJS
Module HTML
directive HTML tag , Angular js
ng-app directive
<html ng-app="store">
Expressions
Allow you to insert dynamic values into your HTML
<p>
I am {{ 4+6 }}
</p>
<p>
I am 10
</p>
RAMP UP 5
AngularJS
<p>
{{"hello" + " you"}}
</p>
<p>
hello you
</p>
RAMP UP 6
AngularJS
Objectives
Create a Module named gemStore so we can get started on this marketing journey.
Attach the gemStore module to our HTML page with a Directive.
In index.html, create a simple Expression to display a friendly "Hello, Angular!" message.
(function(){
var app=angular.module('gemStore',[]);
})()
<html ng-app="gemStore">
<head>
<meta charset="utf-8">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<p>
{{"Hello," + " Angular"}}
</p>
</div>
var gems = {
name: '1',
price: 6.95,
description: '...'
}
json , html ?
controller
controller?
(function(){
var app=angular.module('gemStore',[]);
app.controller('StoreController',function(){
});
})()
controller
(function(){
var app=angular.module('gemStore',[]);
app.controller('StoreController',function(){
this.product = gem; // product
});
var gem = {
name: '1',
price: 6.95,
description: '...'
}
})()
ng-controller="controller as "
</div>
<p> {{store.product.description}} </p>
Built-in Directives
directives
var gem = {
name: '1',
price: 6.95,
description: '...',
canPurchase: false
}
ng-show
, canPurchase
ng-show true ,
<button ng-show="{{store.product.canPurchase}}">Buy</button>
</div>
ng-show , false
var gem = {
name: '1',
price: 6.95,
description: '...',
}
, canPurchase ,
ng-hide
soldOut , false
var gem = {
name: '1',
price: 6.95,
description: '...',
canPurchase: false,
soldOut: true
Built-in Directives 10
AngularJS
<button ng-show="{{store.product.canPurchase}}">Buy</button>
</div>
</div>
ng-hide
<div ng-hide="store.product.soldOut">
,,
app.controller('StoreController',function(){
this.products = gems;//
});
//
var gems =[
{
name: '1',
price: 6.95,
description: '...',
canPurchase: false
},
{
name: '2',
price: 3.74,
description: '...',
canPurchase: true
}
];
,,
Built-in Directives 11
AngularJS
, ng-repeat
ng-repeat
ng-repeat
Built-in Directives 12
AngularJS
{{product.price | currency}}
{{ data | filter:options }}
12/27/2013 @ 1:50PM
OCTAGON GEM
My Descr
images: [
{
full: 'images/1.jpg',
thumb: 'images/thumb1.jpg'
},
{
full: 'images/2.jpg',
thumb: 'images/thumb2.jpg'
}
]
1 {{product.images[0].full}}
<img src="{{product.images[0].full}}"/>
, 404 error
ng-click
, tab
tab
ng-click , ,
panel tab
3 panel ,,
<div class="panel">
<h4>Description</h4>
<p>{{ product.description }}</p>
</div>
<div class="panel">
<h4></h4>
<blockquote></blockquote>
</div>
<div class="panel">
<h4></h4>
<blockquote></blockquote>
</div>
panel
ng-init
logic ,
logic controller
controller PanelController
ng-controller="PanelController as panel"
js
app.controller("PanelController",function(){
});
1. ng-init="tab=1" controller
app.controller("PanelController",function(){
this.tab=1;
});
2. ng-click , function
this.setTab = function(newValue){
this.tab = newValue;
};
3. ng-class
this.isSet = function(t){
return this.tab === t;
};
4. panel ng-show
Accepting Submissions
Accepting Submissions 18
AngularJS
HTML
required
<fieldset class="form-group">
<input ng-model="reviewCtrl.review.author" required type="email" class="form-control"
</fieldset>
</form>
ng-submit , formname.$valid
angular $valid ,,
.ng-invalid.ng-dirty{border-color:red}
.ng-valid.ng-dirty{border-color:green}
Directives
,
ng-include ,
product-description.html
<div>
<h4>Description</h4>
<blockquote>{{product.description}}</blockquote>
</div>
index.html
<div ng-include="'product-description.html'">
</div>
directive
Element
app.js
app.directive("productDescription",function(){
return{
restrict:'E',//element
templateUrl:"product-description.html"
};
});
index.html
<product-description></product-description>
Attribute
app.js
Directives 20
AngularJS
app.directive("productSpecs", function() {
return {
restrict: "A",
templateUrl: "product-specs.html"
};
});
index.html
<div product-specs></div>
controller directive
app.directive("productTabs",function(){
return{
restrict:"E",
templateUrl:"product-tabs.html" ,
controller:function(){
this.tab = 1;
this.isSet = function(checkTab) {
return this.tab === checkTab;
};
this.setTab = function(setTab) {
this.tab = setTab;
};
},
controllerAs:"tab"
};
});
product-tabs.html
<section>
<ul class="nav nav-pills">
<li ng-class="{ active:tab.isSet(1) }">
<a href ng-click="tab.setTab(1)">Description</a>
</li>
<li ng-class="{ active:tab.isSet(2) }">
<a href ng-click="tab.setTab(2)">Specs</a>
</li>
<li ng-class="{ active:tab.isSet(3) }">
<a href ng-click="tab.setTab(3)">Reviews</a>
</li>
</ul>
Directives 21
AngularJS
index.html
<product-tabs></product-tabs>
Directives 22
AngularJS
Form Model
review
reviews
reviews:[
{
stars:5,
body:"",
author:mkdodos@gmail.com
},
{
stars:2,
body:"",
author:dada@gmail.com
},
]
reviews
app.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{%);
$interpolateProvider.endSymbol(%}');
});
First Chapter 23
AngularJS
json
module1 : controller
angular.module('caseCtrl', [])
.controller('mainController', function($scope, $http, Case) {
// service
Case.get()
.success(function(data) {
$scope.rows = data;// rows
});
});
module2 : service
angular.module('caseService', [])
.factory('Case', function($http) {
return {
get : function() {
return $http.get('shop/json');//
}
}
});
laravel controller
module
json 24
AngularJS
</div>
json 25
AngularJS
custom filter
,
.filter('sumPrice',function(){
return function(data){
//
}
})
{% rows | sumPrice %}
angular.module('shopCtrl', [])
.controller('shopController', function($scope, $http, Shop) {
// service
Shop.get()
.success(function(data) {
$scope.rows = data;// rows
console.log(data);
});
})
//*
.filter('sumPrice',function(){
return function(data){
var total=0;
for(var i=0;i<data.length;i++){
if(data[i].qty>0)
total+=data[i].price*data[i].qty;
}
return total;
}
})
;
custom filter 26
AngularJS
</thead>
<tbody>
</tbody>
</table>
</div>
custom filter 27
AngularJS
select ng-options
http://blog.miniasp.com/post/2013/05/12/AngularJS-ng-module-select-ngOptions-usage-samples.aspx
id
{% cust.id %}
angular.module('custCtrl', [])
.controller('custController', function($scope, $http, Cust) {
// service
Cust.get()
.success(function(data) {
$scope.rows = data;//
});
})
;
angular.module('custService', [])
.factory('Cust', function($http) {
return {
get : function() {
return $http.get('../customers2/json');//
}
}
});
json
$rows=Customer::all();
[
{
"id": 1,
"name": "",
"tel": "1234-5789"
},
{
"id": 3,
"name": "",
"tel": "1235-8987"
select ng-options 28
AngularJS
}
]
select ng-options 29