Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AngularJS

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for gaozhitw gaozhitw
May 28, 2014
130

 AngularJS

Avatar for gaozhitw

gaozhitw

May 28, 2014
Tweet

Transcript

  1. 特點 • Two-Way Data Binding • Module Dependency Injection •

    宣告式語法Directive(HTML的元素、屬性、註解) • 採⽤用MVC Design Pattern • DOM Templates • Single Page Application(SPA)
  2. Data Binding 假設Controller內有⼀一個$scope property “body” $scope.body = ‘content’; 結繫到View Render的⽅方式(One-Way):

    • 利⽤用兩個⼤大括號{{body}} • 利⽤用ng-bing Directive 如:<span ng-bind=“body”></span> 前者在Angular compile完成前,會看到{{body}}的字串,若不想讓使⽤用者看 到compile完成前的畫⾯面,建議使⽤用第⼆二種⽅方式
  3. 結繫到Form Element的⽅方式(Two-Way): 利⽤用ng-model Directive,如: JS: $scope.inputText = ‘XD’; HTML: <input

    type=“text” ng-model=“inputText” /> 當使⽤用者在input輸⼊入⽂文字後,將會⽴立即改變$scope.inputText的 值 ⽽而在HTML上若有 {{inputText}} 也將⽴立即render為新的資料
  4. rootScope Scope Child Scope Child Scope $scope.title = ‘XD’ HTML

    Form Input: ng-model=“title” (Two-Way Data Binding)
  5. rootScope Scope Child Scope Child Scope $scope.title = ‘XD’ HTML

    Form Input: ng-model=“title” (Two-Way Data Binding) 建⽴立$scope.title = ‘XDD’ XD XDD <input type=“text” ng-model=“title” />
  6. rootScope Scope Child Scope Child Scope $scope.title = ‘XD’ HTML

    Form Input: ng-model=“title” (Two-Way Data Binding) XD <input type=“text” ng-model=“title” /> 解法⼀一: <input type=“text” ng-model=“$parent.$parent.title” />
  7. rootScope Scope Child Scope Child Scope $scope.formData = {title: ‘XD’};

    HTML Form Input: ng-model=“title” (Two-Way Data Binding) XD <input type=“text” ng-model=“title” /> 解法⼆二:(推薦) <input type=“text” ng-model=“formData.title” />
  8. Scope Watch $scope.$watch(watchExpression, [listener], [objectEquality]) • watchExpression(string or function): (scope

    property)
 ex: $scope.$watch(‘body’); • listener(function): (callback function) • objectEquality(boolean): 深⼊入監看Object內容是 否相等
  9. 範例: MyCtrlMod.controller(‘ExCtrl’, [$scope, function($scope){
 $scope.demo = ‘example’;
 
 $scope.obj =

    {
 obj1: ‘objExample1’,
 obj2: ‘objExample2’
 };
 
 $scope.watch(‘demo’, function(){
 do_something;
 });
 
 $scope.watch(‘obj’, function(){
 do_something;
 }, true);
 }]);
  10. 需要注意的地⽅方: watch在angular compile完成後,會先動作⼀一次, 若只想在property變更時才動做,可在callback function內加⼊入: $scope.$watch(‘example’, function(newValue, oldValue)){
 if(newValue ===

    oldValue){
 return; 
 }
 do_something;
 } watch的callback function還有第三個可選的參數,可傳⼊入scope
 進⾏行所需的操作,例: $scope.$watch(‘example’, function(newValue, oldValue, scope)..
  11. Angular Directive 常⾒見(使⽤用)Angular內建Directive: • ngApp • ngController • ngBind •

    ngRepeat、ngInit • ngShow、ngHide • ngIf • ngClass • ngClick、ngChange、ngKeydown、ngKeypress…. • ngSubmit
  12. <html>
 …..
 <body>
 <div ng-repeat=“name in nameList”>
 {{name}}
 </div>
 </body>


    </html> ——————————————————————
 ⼤大明
 ⼩小偉
 阿忘
  13. ngShow / ngHide • 假如ngShow結繫到的property為true,則CSS display: normal; 顯⽰示該Element • 假如ngHide結繫到的property為true,則CSS

    display: none; 隱藏該Element 例:
 $scope.showElement = true; <div ng-show=“showElement”></div>
  14. ngClass 結繫到的property為true時,寫⼊入className到該 Element的class attribute 例: <div class=“mo” ng-class=“{co: addco, do:

    adddo}”></div> $scope.addco = true;
 $scope.adddo = false; render:
 <div class=“mo co”></div>
  15. Event Directive ngClick、ngChange…… 可以結繫到method,如:
 $scope.action = function($event){ do_something };
 


    <button type=“button” ng-click=“action($event)”></button> 可選擇性傳⼊入$event 引數,取得⼀一些event的屬性
  16. Angular Service 常⾒見內建Service: • $filter • $http • $window •

    $location • $document • $timeout • $interval
  17. • 直接在HTML Template Binding中使⽤用 例:
 <div ng-repeat=“name in nameList |

    filter: [String|Object|function(value)]”>
 {{name}}
 </div> • (String) Array或Object內有符合的String才會被回傳 • (Object) 如{no: “1”, name: “wayne”},將回傳符合條件的Object • (function) 會將要判斷的值,當成第⼀一個引數傳進method,假如 method回傳true,則回傳該值。 在HTML Template中,也可將資料格式化,如: {{ dateObj | date : ‘yyyy-MM-dd’ }}
 {{ Obj | json }}
  18. • 將Service注⼊入Javascript中使⽤用 MyCtrlMod.controller(‘MyCtrl’, [‘$scope’, ‘$filter’, function($scope, $filter){
 $filer(‘filter’)(array, function(value){
 if(….){


    return false; //過濾掉
 }
 return true;
 });
 }]); 也能做資料格式化,如: $filter(‘json’)(Object);
 $filter(‘date’)(date, format);
  19. $http AngularJS中⽤用來發出Http Resquest的Service MyCtrlMod.controller(‘MyCtrl’, [‘$scope’, ‘$http’, function($scope, $http){
 $http({method: ‘GET’,

    url: ‘/someUrl’})
 .success(function(data, status, headers, config){
 do_something;
 }).error(function(data, status, headers, config){
 do_something;
 });
 }]); 通常在Web Application開發時,Http Request通常不會⽤用到如此細鎖的操 作,所以Angular官⽅方有另外將這些操作進⾏行⾼高階抽象化成⼀一個module “ngResource”,再⼤大部份的情況下使⽤用該module操作即可。
  20. $timeout / $interval 這組Service是打包原⽣生Javascript得setTimeout與 setInterval 例: MyCtrlMod.controller(‘MyCtrl’, [‘$scope’, ‘$timeout’, ‘$interval’,

    function($scope, $timeout, $interval){
 $timeout(function(){
 do_something;
 }, 2000);
 
 $interval(function(){
 do_something;
 }, 2000, 5);
 }]);
  21. Scope Apply 為什麼需要將Native Javascript打包成Service? 舉setTimeout為例:
 MyCtrlMod.controller(‘MyCtrl’, [‘$scope’, function($scope){
 $scope.face =

    ‘XD’;
 
 setTimeout(function(){
 $scope.face = ‘:)’;
 }, 2000); $scope.$watch(‘face’, function(newValue, oldValue){
 if(newValue === oldValue){ return };
 alert(‘smile’);
 });
 }]); HTML Template binding:
 <div>{{face}}</div>
  22. 打包成Service的⽤用意: • 包含調⽤用$scope.$apply() • 使code可測試 因此最後可改為:
 MyCtrlMod.controller(‘MyCtrl’, [‘$scope’, ‘$timeout’, function($scope,

    $timeout){
 $scope.face = ‘XD’;
 
 $timeout(function(){
 $scope.face = ‘:)’;
 }, 2000); $scope.$watch(‘face’, function(){
 alert(‘smile’);
 });
 }]);
  23. ngResource Module • ngResource module包含了⼀一個$resource Service
 將$http Service打包成更加抽象⽅方便使⽤用的XHR Service •

    基於RESTful的概念去設計 使⽤用: <script src=“angular-resource.js”></script>
 
 angular.module(‘MyApp’, [‘MyCtrlModule’, ‘MyServiceModule’]);
  24. 創建⼀一個Service Module
 angular.module(‘MyServiceModule’, [‘ngResource’]) .factory(‘MyData’, [‘$resource’, function($resource){
 var getNameData =

    $resource(‘/someUrl’);
 return {getName: getNameData};
 }]); 在Controller內使⽤用
 angular.module(‘MyCtrlModule’, []) .controller(‘MyCtrl’, [‘$scope’, ‘MyData’, function($scope, MyData){
 var nameList = MyData.getName.get({}, function(){
 do_success_something;
 $scope.nameList = nameList;
 }, function(){
 do_error_something;
 });
 }]);
  25. 預設RESTful Method: {‘get’: {method: ‘GET’},
 ‘save’: {method: ‘POST’},
 ‘query’: {method:

    ‘GET’, isArray: true},
 ‘reomve: {method: ‘DETELE’},
 ‘detele’: {method: ‘DETELE’} }; ⾃自⾏行定義Method: $resource(‘/someUrl’, {}, {
 someMethod: {method: ‘GET’, isArray: true, timeout: 30000, ……}
 }); 可變網址參數:
 $resource(‘/someUrl/:Id’, {Id: 5});
 MyData.getName.get({Id: 5}, function(){…..}, function(){.….});