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

AngularJS training in Luster

AngularJS training in Luster

2014 達暉資訊教育訓練課程 - AngularJS

課程簡介:
想要開發複雜的互動網頁,卻被拉哩拉咂的 JS 和 HTML 搞得暈頭轉向?

還在為處理大量的動態資料顯示感到厭煩?

想猶如造物主般的自己創造自己的 HTML 標籤?

來試試 AngularJS 吧 ~
*MVVM 讓你的 Code 清爽又乾淨。
*2-way Data Binding 保證你藥到病除。
*Directive 讓你擴展 HTML,享受自在的樂趣。

AngularJS 是一個極其優美的前端架構,使用它,讓你的開發路更有趣,更有成就感~

shiningjason1989

March 08, 2014
Tweet

More Decks by shiningjason1989

Other Decks in Technology

Transcript

  1. Observer About MVC View UI, Represents current model state Controller

    Make decision for the View Model Domain-specific data User Input Modify Display Output Notify Handle event
  2. About MVVM View User Interface View Model State and Operations

    Data bindings and Commands Send notifications Update Model Domain-specific data Send notifications
  3. What’s the problem in JS & jQuery?(多選) 1. 當需求增加,程式碼可能重複 2.

    難以得知某個 HTML element 的所有⾏行為 3. 輸入了 “Hello, ...”,無法同時顯⽰示在不同的 <DIV> 上 4. 改變了某個 HTML element 的 id 或 class,可能照成無法 預期的錯誤 5. 使⽤用 jQuery 的 selector 很難 debug
  4. Can not <div id=“output”> $(“#input”).keyup(function () { $(“#output”).html(…); }); <input

    id=“input”> <div id=“output1”> $(“#input1”).keyup(function () { $(“#output1”).html(…); }); <input id=“input1”> <div id=“output2”> $(“#input2”).keyup(function () { $(“#output2”).html(…); }); <input id=“input2”>
  5. $(“#input1”).keyup(function () { var val = $(this).val(); ! $(“#output1”).html(val); !

    }); <div id=“output1”> <input id=“input1”> 如果需求改變...
  6. <div id=“output1”> $(“#input1”).keyup(function () { var val = $(this).val(); !

    $(“#output1”).html(val); ! }); <input id=“input1”> <input id=“input2”> $(“#input2”).keyup(function () { var val = $(this).val(); ! $(“#input1”).val(val) $(“#output1”).html(val); }); $(“#input2”).val(val); 如果需求改變...
  7. What’s the problem in jQuery 維護 id/class 在幹嘛? 加班! 1.

    到底為了什麼維護 id/class? 2. 操作 DOM,因為要處理很多的 UI 互動 有時候無法從 HTML 中,看出元素的行為 3.
  8. VS Two way data binding DOM manipulation 我們必須處理很多 event 我們專注

    data 編程式的開發思維 宣告式的開發思維 很多很棒的 function 適合處理資料與 UI 的互動 IE 支援 6 以上 IE 支援 8 以上
  9. <body ng-app> <div ng-controller=“UncleCtrl”> <div ng-controller=“FatherCtrl”> {{ money }} =>

    1億 $scope.money = ‘2億’; {{ money }} => 2億 {{ money }} => 1億 $rootScope.money = ‘1億’;
  10. <body ng-app> <div ng-controller=“UncleCtrl”> <div ng-controller=“FatherCtrl”> {{ money }} =>

    1億 $scope.money = ‘2億’; {{ money }} => 2億 {{ money }} => 1億 $rootScope.money = ‘1億’; $scope.wife = ‘林依晨’; {{ wife }} => 林依晨 $scope.wife = ‘桂綸鎂’; {{ wife }} => 桂綸鎂
  11. 3. Scope 之間的關係就像 爺爺 ⼤大伯 ⼆二伯 ⽗父親 堂哥 我 ⼩小弟

    親屬關係 可繼承父親屬性 兄弟之間的屬性不會影響
  12. Mrs. Controller app.controller(‘fatHottyCtrl’, function ($scope, $http) { $scope.findAllHotties = function

    () { $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; } );
  13. Mrs. Controller app.controller(‘fatHottyCtrl’, function ($scope, $http) { $scope.findAllHotties = function

    () { $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; } ); Mrs. Controller 2 app.controller(‘fatHottyCtrl2’, function ($scope, $http) { $scope.findAllHotties = function () { $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; } );
  14. app.controller(‘fatHottyCtrl’, function ($scope, $http) { $scope.findAllHotties = function () {

    $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; } ); app.controller(‘fatHottyCtrl2’, function ($scope, $http) { $scope.findAllHotties = function () { $http.get(…).then(…); }; $scope.findOneHotty = …; $scope.createHotty = …; $scope.updateHotty = …; $scope.deleteHotty = …; app.service(‘hottyService’, function ($http) { this.findAllHotties = function () { $http… }; this.findOneHotty = …; this.createHotty = …; this.updateHotty = …; this.deleteHotty = …; } );
  15. app.controller(‘thinHottyCtrl2’, function ($scope, hottyService) { $scope.hottyService = hottyService; } );

    app.controller(‘thinHottyCtrl’, function ($scope, hottyService) { $scope.hottyService = hottyService; } ); app.service(‘hottyService’, function ($http) { this.findAllHotties = function () { $http… }; this.findOneHotty = …; this.createHotty = …; this.updateHotty = …; this.deleteHotty = …; } );
  16. ·• Angular built-in service 像 $http、$location 等 ·• Angular 提供我們很多方法創建自己的

    Service Services 備註 module.service() module.provider() module.factory() = 不同方法,但都是為了創建 Service
  17. app.controller(‘thinHottyCtrl’, function ($scope, hottyService) { $scope.hottyService = hottyService; } );

    Angular 會幫你管理物件的生命週期 你只需要很明確的跟它要 名稱必須正確
  18. angular.module(‘a’, [‘b’]) .service(‘aService’, function (bService) {} ); 找人,總是要那個人曾經活過 確保它是存在的 angular.module(‘b’,

    []) .service(‘bService’, function () {} ); 所以,如果你要的東西定義在其他模組, 請記得要 import 那個模組
  19. DI 的好處 app.controller(‘thinHottyCtrl1’, function ($scope, hottyService) {…}); app.controller(‘thinHottyCtrl2’, function ($scope,

    hottyService) {…} function HottyServiceA() {…}; app.service(‘hottyService’, HottyServiceA);
  20. 鬆耦合 app.controller(‘thinHottyCtrl1’, function ($scope, hottyService) {…}); app.service(‘hottyService’, HottyServiceB); function HottyServiceA()

    {…}; app.controller(‘thinHottyCtrl2’, function ($scope, hottyService) {…} function HottyServiceB() {…};
  21. Now, you know How to use it Scopes Controllers Services

    Directives Dependency Injection …