Page 1
เริ่มต้น Angular JS แบบสบายๆ
โดย ธีรเศรษฐ์ จิรภัทร์ชาญเดช
Technology Coaching www.nextflow.in.th/angularjs
www.nextflow.in.th/ionic-framework
Page 2
www.nextflow.in.th/angularjs
“พล” Teerasej JiraphatchandejTechnology Coach Nextflow.in.th
Email: [email protected] facebook.com/nextflow www.nextflow.in.th
Page 3
www.nextflow.in.th/angularjs
ทำไมต้อง Angular JS?
Page 4
www.nextflow.in.th/angularjs
HTML
ข้อมูล …
Database Web service
Object Array
Page 5
www.nextflow.in.th/angularjs
JQuery หรือ
Angular JS
Page 6
www.nextflow.in.th/angularjs
$(selector).method()<div id=“main”>
<input class=“b”/>
</div>
Page 7
www.nextflow.in.th/angularjs
data-object {{data-object}}
Page 8
www.nextflow.in.th/angularjs
Page 9
www.nextflow.in.th/angularjs
HTML
Module
Controller$scope.property
$scope.method()
Page 10
www.nextflow.in.th/angularjs
Page 11
www.nextflow.in.th/angularjs
Page 12
www.nextflow.in.th/ionic-framework
module
Page 13
www.nextflow.in.th/angularjs
Module
Page 14
www.nextflow.in.th/ionic-framework
angular.module(“myApp”, [] )
Page 15
www.nextflow.in.th/ionic-framework
js/app.js
Page 16
www.nextflow.in.th/angularjs
Module
Page 17
www.nextflow.in.th/angularjs
Page 18
www.nextflow.in.th/ionic-framework
directive
Page 19
www.nextflow.in.th/angularjs
<script
src=“js/app.js”>
HTML
app.js .module(“myApp”)
<body ng-app=“myApp”>
Page 20
www.nextflow.in.th/angularjs
<body ng-app=“myApp”>
myApp = angular.module(“myApp”, [] );
Page 21
www.nextflow.in.th/ionic-framework
myApp = angular.module(“myApp”, [] )
Page 22
www.nextflow.in.th/ionic-framework
Controller
Page 23
www.nextflow.in.th/ionic-framework
.controller(“…”, [‘$scope’, function($scope){
// magic!
} ] );
Page 24
www.nextflow.in.th/angularjs
.controller($scope) {
$scope.name = “Pon” }
Page 25
www.nextflow.in.th/ionic-framework
$scope
Page 26
www.nextflow.in.th/angularjs
.controller( function(){ $scope.property = [];
}]);
Page 27
www.nextflow.in.th/ionic-framework
Expression
Page 28
www.nextflow.in.th/angularjs
Controller “MyCtrl”
HTML
<… ng-controller=“MyCtrl”>
My name is {{ username }}
Page 29
www.nextflow.in.th/angularjs
<h1> Hello, {{ username }}</h1>
.controller( function(){ $scope.username = “Pon”;
}]);
Page 30
www.nextflow.in.th/ionic-framework
directive
Page 31
www.nextflow.in.th/ionic-framework
ng-click
Page 32
www.nextflow.in.th/angularjs
<button ng-click=“doSomething(param)”>…</button>
.controller( function(){ $scope.doSomething = function(param){ };
}]);
Page 33
www.nextflow.in.th/angularjs
<button ng-click=“sayHi()”>
HTML
Controller $scope.sayHi()
Page 34
www.nextflow.in.th/angularjs
WorkshopC1: Vat 7 Controller
Page 35
www.nextflow.in.th/ionic-framework
ng-repeat
Page 36
www.nextflow.in.th/ionic-framework
$scope.list = [ 1,2,3
];
Page 37
www.nextflow.in.th/angularjs
<ul ng-repeat=“item in list”> <li>{{item}}</li>
</ul>
HTML
Controller $scope.list
Page 38
www.nextflow.in.th/ionic-framework
$scope.list = [ { id: 1, ch: ‘a’}, { id: 2, ch: ‘b’}, { id: 3, ch: ‘c’},
];
Page 39
www.nextflow.in.th/angularjs
<ul ng-repeat=“item in list”> <li>{{item.name}}</li>
</ul>
HTML
Controller $scope.list
Page 40
www.nextflow.in.th/ionic-framework
Challenge:
Page 41
www.nextflow.in.th/ionic-framework
filter
Page 42
www.nextflow.in.th/ionic-framework
ng-view + ngRoute
Page 43
www.nextflow.in.th/angularjs
index.html Controller
Page 44
www.nextflow.in.th/angularjs
Routeng-view
Page 45
www.nextflow.in.th/angularjs
HTML (/) HomeController
HTML (/profile) ProfileController
Route
Page 46
www.nextflow.in.th/ionic-framework
$routeParams
Page 47
www.nextflow.in.th/angularjs
HTML (/profile/:id)
ProfileController $routeParams.id
Route
href=“#/profile/29
Page 48
www.nextflow.in.th/ionic-framework
Factory
Page 49
www.nextflow.in.th/angularjs
แชร์ข้อมูลระหว่าง Controller
HTML
Controller Data, Web Service
Controller
Controller
Data, Web Service
Data, Web Service
Page 50
www.nextflow.in.th/angularjs
แชร์ข้อมูลระหว่าง Controller
HTML
Controller
FactoryController
Controller
Page 51
www.nextflow.in.th/angularjs
.factory( ‘Name’ , [ ‘’ , function() {
}]);
Page 52
www.nextflow.in.th/angularjs
.factory( function() { return {
method1 : function() {
} }
}]);
Page 53
www.nextflow.in.th/angularjs
.factory( ‘UserService’ … );
.controller( … [ ‘UserService’ , function( UserService ) {
}]);
Page 54
www.nextflow.in.th/ionic-framework
Web Service
Page 55
www.nextflow.in.th/ionic-framework
$http.post $http.get
Page 56
www.nextflow.in.th/ionic-framework
$http.get(‘url’) .then( function(resp){ … }, function(error){ … } )
Page 57
www.nextflow.in.th/ionic-framework
$http.post(‘url’, {} ) .then( function(resp){ … }, function(error){ … } )
Page 58
ขอบคุณครับ โดย ธีรเศรษฐ์ จิรภัทร์ชาญเดช
Technology Coaching www.nextflow.in.th/angularjs
www.nextflow.in.th/ionic-framework
Page 59
www.nextflow.in.th/angularjs
“พล” Teerasej JiraphatchandejTechnology Coach Nextflow.in.th
Email: [email protected] facebook.com/nextflow www.nextflow.in.th