多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

中國最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2

angularjs教程

AngularJS 開始入門

閱讀 (2573)

快速入門

為什么要用AngularJS?

HTML非常適合于聲明靜態的文檔,但是當我們試圖使用它在web應用程序中聲明動態視圖時,它顯得力不從心。AngularJS能為您的應用程序擴展HTML的詞匯。由此產生的環境非常具有有表現力、可讀性強、快速發展。

替代選擇

其他處理HTML的缺點的框架要么是抽象出HTML、CSS、和/或JavaScript,要么為操縱DOM提供一個必要的方式。它們都不能解決一個根本問題,即HTML不是為動態視圖設計的。

可擴展性

AngularJS是用來構建框架的工具集,很適全于你的應用程序開發。它完全可擴展,而且與別的庫協作得很好。每個功能可以被修改或替代,以適合你的獨一無二的開發工作流以及功能需要。繼續閱讀以弄懂為何。

The Basics

index.html

<!doctype html>
<html ng-app>
  <head>
    <script src="/attachments/image/wk/angularjs/angular.min.js"></script>
  </head>
  <body>
    <div>
      <label>Name:</label>
      <input type="text" ng-model="yourName" placeholder="Enter a name here">
      <hr>
      <h1>Hello {{yourName}}!</h1>
    </div>
  </body>
</html>

添加一些控件

數據綁定

數據綁定是每當模型改變時更新視圖的自動方法,當視圖改變時,同樣也會更新模型。這非常棒,因為從你需要擔心的列表中它減去了DOM操縱。

控件

控件是DOM元素后面的行為。AngularJS讓你能夠用一個干凈可讀的形式表達行為,不需要更新DOM的通常樣板、注冊回調或者觀察模型變化。

扁平的JavaScript

與別的框架不同,不需要為包裝訪問器方法中的模型,而繼承私有類型。Angular模型是扁平的舊式JavaScript對象。這使你的代碼容易讀取、容易維護、可重用,還不需要樣板。

index.html

<!doctype html>
<html ng-app="todoApp">
  <head>
    <script src="/attachments/image/wk/angularjs/angular.min.js"></script>
    <script src="todo.js"></script>
    <link rel="stylesheet" href="todo.css">
  </head>
  <body>
    <h2>Todo</h2>
    <div ng-controller="TodoListController as todoList">
      <span>{{todoList.remaining()}} of {{todoList.todos.length}} remaining</span>
      [ <a href="" ng-click="todoList.archive()">archive</a> ]
      <ul class="unstyled">
        <li ng-repeat="todo in todoList.todos">
          <input type="checkbox" ng-model="todo.done">
          <span class="done-{{todo.done}}">{{todo.text}}</span>
        </li>
      </ul>
      <form ng-submit="todoList.addTodo()">
        <input type="text" ng-model="todoList.todoText"  size="30"
               placeholder="add new todo here">
        <input class="btn-primary" type="submit" value="add">
      </form>
    </div>
  </body>
</html>

todo.js

angular.module('todoApp', [])
  .controller('TodoListController', function() {
    var todoList = this;
    todoList.todos = [
      {text:'learn angular', done:true},
      {text:'build an angular app', done:false}];

    todoList.addTodo = function() {
      todoList.todos.push({text:todoList.todoText, done:false});
      todoList.todoText = '';
    };

    todoList.remaining = function() {
      var count = 0;
      angular.forEach(todoList.todos, function(todo) {
        count += todo.done ? 0 : 1;
      });
      return count;
    };

    todoList.archive = function() {
      var oldTodos = todoList.todos;
      todoList.todos = [];
      angular.forEach(oldTodos, function(todo) {
        if (!todo.done) todoList.todos.push(todo);
      });
    };
  });

todo.css

.done-true {
  text-decoration: line-through;
  color: grey;
}

后端連接

深鏈接

一個深鏈接反應了用戶在應用中的哪個位置,這很有用,所以用戶可以把它存為書簽以及電子郵件鏈接,以在應用內部定位它。往返旅行的應用程序會自動獲得這個功能,但Ajax應用程序按其性質不會。AngularJS結合了深鏈接以及類似桌面應用程序的行為的優點。

表單驗證

客戶端表單驗證是完美的用戶體驗的一個重要的部分。AngularJS使你能夠聲明表單的有效性規則,而不需要書寫JavaScript代碼。從而事半功倍。

服務器通信

AngularJS提供了內建的建在在XHR的頂層的服務,以及多種多樣的使用第三方庫的其它后端。通過處理異步返回的數據,Promise進一步簡化了您的代碼。在這個示例中,我們使用AngularFire庫以把一個Firebase后端接通到一個簡單的Angular應用上。

index.html

<!doctype html>
<html ng-app="project">
  <head>
    <script src="/attachments/image/wk/angularjs/angular.min.js"></script>
    <script src="/attachments/image/wk/angularjs/angular-resource.min.js">
    </script>
    <script src="/attachments/image/wk/angularjs/angular-route.min.js">
   </script>
    <script src="/attachments/image/wk/angularjs/firebase.js"></script>
    <script src="/attachments/image/wk/angularjs/angularfire.min.js"></script>
    <link rel="stylesheet" href="bootstrap.css">
    <script src="project.js"></script>
  </head>
  <body>
    <h2>JavaScript Projects</h2>
    <div ng-view></div>
  </body>
</html>

bootstrap.css

// Uncomment this in Plunker or JSFiddle: @import '//netdna.bootstrapcdn.com/twitter-bootstrap/2.0.4/css/bootstrap-combined.min.css';

project.js

angular.module('project', ['ngRoute', 'firebase'])

.value('fbURL', 'https://ng-projects-list.firebaseio.com/')
.service('fbRef', function(fbURL) {
  return new Firebase(fbURL)
})
.service('fbAuth', function($q, $firebase, $firebaseAuth, fbRef) {
  var auth;
  return function () {
      if (auth) return $q.when(auth);
      var authObj = $firebaseAuth(fbRef);
      if (authObj.$getAuth()) {
        return $q.when(auth = authObj.$getAuth());
      }
      var deferred = $q.defer();
      authObj.$authAnonymously().then(function(authData) {
          auth = authData;
          deferred.resolve(authData);
      });
      return deferred.promise;
  }
})

.service('Projects', function($q, $firebase, fbRef, fbAuth) {
  var self = this;
  this.fetch = function () {
    if (this.projects) return $q.when(this.projects);
    return fbAuth().then(function(auth) {
      var deferred = $q.defer();
      var ref = fbRef.child('projects-fresh/' + auth.auth.uid);
      var $projects = $firebase(ref);
      ref.on('value', function(snapshot) {
        if (snapshot.val() === null) {
          $projects.$set(window.projectsArray);
        }
        self.projects = $projects.$asArray();
        deferred.resolve(self.projects);
      });

      //Remove projects list when no longer needed.
      ref.onDisconnect().remove();
      return deferred.promise;
    });
  };
})

.config(function($routeProvider) {
  var resolveProjects = {
    projects: function (Projects) {
      return Projects.fetch();
    }
  };

  $routeProvider
    .when('/', {
      controller:'ProjectListController as projectList',
      templateUrl:'list.html',
      resolve: resolveProjects
    })
    .when('/edit/:projectId', {
      controller:'EditProjectController as editProject',
      templateUrl:'detail.html',
      resolve: resolveProjects
    })
    .when('/new', {
      controller:'NewProjectController as editProject',
      templateUrl:'detail.html',
      resolve: resolveProjects
    })
    .otherwise({
      redirectTo:'/'
    });
})

.controller('ProjectListController', function(projects) {
  var projectList = this;
  projectList.projects = projects;
})

.controller('NewProjectController', function($location, projects) {
  var editProject = this;
  editProject.save = function() {
      projects.$add(editProject.project).then(function(data) {
          $location.path('/');
      });
  };
})

.controller('EditProjectController',
  function($location, $routeParams, projects) {
    var editProject = this;
    var projectId = $routeParams.projectId,
        projectIndex;

    editProject.projects = projects;
    projectIndex = editProject.projects.$indexFor(projectId);
    editProject.project = editProject.projects[projectIndex];

    editProject.destroy = function() {
        editProject.projects.$remove(editProject.project).then(function(data) {
            $location.path('/');
        });
    };

    editProject.save = function() {
        editProject.projects.$save(editProject.project).then(function(data) {
           $location.path('/');
        });
    };
});

list.html

<input type="text" ng-model="projectList.search" class="search-query" id="projects_search"
       placeholder="Search">
<table>
  <thead>
  <tr>
    <th>Project</th>
    <th>Description</th>
    <th><a href="#/new"><i class="icon-plus-sign"></i></a></th>
  </tr>
  </thead>
  <tbody>
  <tr ng-repeat="project in projectList.projects | filter:projectList.search | orderBy:'name'">
    <td><a ng-href="{{project.site}}" target="_blank">{{project.name}}</a></td>
    <td>{{project.description}}</td>
    <td>
      <a ng-href="#/edit/{{project.$id}}"><i class="icon-pencil"></i></a>
    </td>
  </tr>
  </tbody>
</table>

detail.html

<form name="myForm">
  <div class="control-group" ng-class="{error: myForm.name.$invalid && !myForm.name.$pristine}">
    <label>Name</label>
    <input type="text" name="name" ng-model="editProject.project.name" required>
    <span ng-show="myForm.name.$error.required && !myForm.name.$pristine" class="help-inline">
        Required {{myForm.name.$pristine}}</span>
  </div>

  <div class="control-group" ng-class="{error: myForm.site.$invalid && !myForm.site.$pristine}">
    <label>Website</label>
    <input type="url" name="site" ng-model="editProject.project.site" required>
    <span ng-show="myForm.site.$error.required && !myForm.site.$pristine" class="help-inline">
        Required</span>
    <span ng-show="myForm.site.$error.url" class="help-inline">
        Not a URL</span>
  </div>

  <label>Description</label>
  <textarea name="description" ng-model="editProject.project.description"></textarea>

  <br>
  <a href="#/" class="btn">Cancel</a>
  <button ng-click="editProject.save()" ng-disabled="myForm.$invalid"
          class="btn btn-primary">Save</button>
  <button ng-click="editProject.destroy()"
          ng-show="editProject.project.$id" class="btn btn-danger">Delete</button>
</form>

創建組件

指令

指令是一個獨有而且強大的功能,只在Angular中可用。指令使你能夠發明新的HTML句法、專針對于你的應用程序。

可重用的組件

我們使用指令以創建可重復使用的組件。組件允許你隱藏復雜的DOM結構、CSS以及行為。這使你能夠專注于應用程序要做什么,或者單獨的應用程序看起來如何。

本地化

嚴肅的應用程序的一個重要組成部分是本地化。AngularJS的本地探知篩選器以及阻塞指令使你能夠建立屏蔽,使你的應用程序在所有的地方都可用。

index.html

<!doctype html>
<html ng-app="app">
  <head>
    <script src="/attachments/image/wk/angularjs/angular.min.js"></script>
    <script src="components.js"></script>
    <script src="app.js"></script>
    <link rel="stylesheet" href="bootstrap.css">
  </head>
  <body>
    <tabs>
      <pane title="Localization">
        Date: {{ '2012-04-01' | date:'fullDate' }} <br>
        Currency: {{ 123456 | currency }} <br>
        Number: {{ 98765.4321 | number }} <br>
      </pane>
      <pane title="Pluralization">
        <div ng-controller="BeerCounter">
          <div ng-repeat="beerCount in beers">
            <ng-pluralize count="beerCount" when="beerForms"></ng-pluralize>
          </div>
        </div>
      </pane>
    </tabs>
  </body>
</html>

components.js

angular.module('components', [])

  .directive('tabs', function() {
    return {
      restrict: 'E',
      transclude: true,
      scope: {},
      controller: function($scope, $element) {
        var panes = $scope.panes = [];

        $scope.select = function(pane) {
          angular.forEach(panes, function(pane) {
            pane.selected = false;
          });
          pane.selected = true;
        }

        this.addPane = function(pane) {
          if (panes.length == 0) $scope.select(pane);
          panes.push(pane);
        }
      },
      template:
        '<div class="tabbable">' +
          '<ul class="nav nav-tabs">' +
            '<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
              '<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
            '</li>' +
          '</ul>' +
          '<div class="tab-content" ng-transclude></div>' +
        '</div>',
      replace: true
    };
  })

  .directive('pane', function() {
    return {
      require: '^tabs',
      restrict: 'E',
      transclude: true,
      scope: { title: '@' },
      link: function(scope, element, attrs, tabsController) {
        tabsController.addPane(scope);
      },
      template:
        '<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
        '</div>',
      replace: true
    };
  })

app.js

angular.module('app', ['components'])

.controller('BeerCounter', function($scope, $locale) {
  $scope.beers = [0, 1, 2, 3, 4, 5, 6];
  if ($locale.id == 'en-us') {
    $scope.beerForms = {
      0: 'no beers',
      one: '{} beer',
      other: '{} beers'
    };
  } else {
    $scope.beerForms = {
      0: '?iadne pivo',
      one: '{} pivo',
      few: '{} pivá',
      other: '{} pív'
    };
  }
});

bootstrap.css

// Uncomment this in Plunker or JSFiddle: @import '//netdna.bootstrapcdn.com/twitter-bootstrap/2.0.4/css/bootstrap-combined.min.css';

可測性內置

可注入

在AngularJS依賴性注入允許你聲明式地描述你的應用程序是如何連線的。這意味著你的應用程序不需要main()方法,該方法通常是一個難以維護的大堆雜。依賴性的注入也是AngularJS的一個核心。這意味著任何不適合你的需要的組件可以輕松替換掉。

可測試

AngularJS設計為從根基開始都是可測試的。它鼓勵行為與視圖分離、用預綁定來模擬、充分利用依賴性注入。它還配備了端到端的場景分流道,它通過理解AngularJS的內部運作機制消除了測試片層分享。

關閉
程序員人生
主站蜘蛛池模板: 最近最新中文字幕 | 午夜精品福利影院 | 久久a级片 | 久久er国产精品免费观看8 | 98国内自拍在线视频 | 欧美 日韩 中字 国产 | 精品亚洲一区二区三区 | 国产尤物在线视频 | 亚洲 欧美 日韩 综合aⅴ视频 | 日本午夜视频在线 | 日韩欧美日本 | 欧美毛片视频 | 久久午夜一区二区 | www在线视频在线播放 | 国产欧美日韩在线一区二区不卡 | 精品亚洲综合在线第一区 | 看毛片的网址 | 亚洲欧美日韩国产精品网 | 三浦惠理子中文字幕在线一区二区 | 欧美精品免费一区欧美久久优播 | 亚洲成人影院在线 | 亚洲免费网站 | yellow网站在线观看 | 国产婷婷高清在线观看免费 | 成人免费性视频 | 精品国产午夜久久久久九九 | 亚洲午夜久久久久国产 | 欧美视频一区二区在线观看 | 欧美一区二区在线观看 | 国产性生活视频 | 黄色在线网站视频 | 亚洲欧美日韩中文字幕网址 | 在线不卡免费视频 | 狍和美女一级aa毛片 | 日本特黄特色大片免费播放视频 | 国产第一页在线播放 | 国产精品一区久久 | 免费网址在线观看 | 免费看h视频 | 欧美高清成人videosex | 欧美xxxxx极品|