angularjs1

鏈接

https://www.w3schools.com/angular/

基礎

directives, expressions, filters, modules, and controllers
Events, DOM, Forms, Input, Validation, Http

歷史

AngularJS version 1.0 was released in 2012.

工作原理

AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions.

  1. 通過指令拓展html屬性
    ng-app directive defines an AngularJS application.

ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

ng-bind directive binds application data to the HTML view.

ng-init directive initializes AngularJS application variables.

  1. 使用表達式綁定數據到html
    AngularJS expressions are written inside double braces: {{ expression }}.

    AngularJS expressions bind AngularJS data to HTML the same way as the ng-bind directive.

<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<script>
// AngularJS modules define applications:
var app = angular.module('myApp', []);
// AngularJS controllers control applications:
app.controller('myCtrl', function($scope) {
    $scope.firstName= "John";
    $scope.lastName= "Doe";
});
</script>

AngularJS Expressions

{{ expression }} 或者 ng-bind="expression"

object
ng-init="person={firstName:'John',lastName:'Doe'}"
{{ person.lastName }}

Arrays
ng-init="points=[1,15,19,2,40]"
{{ points[2] }}

AngularJS Modules

The module is a container for the application controllers.

AngularJS Directives

AngularJS lets you extend HTML with new attributes called Directives.

  1. The ng-app Directive
    root element
    auto-bootstrap

  2. The ng-init Directive
    initial values

  3. The ng-model Directive
    bind html values to data

  4. Create New Directives
    js 中 camel case name
    html 中 use - separated name

<w3-test-directive></w3-test-directive>
app.directive("w3TestDirective", function() {
    return {
        template : "<h1>Made by a directive!</h1>"
    };
});

使用范圍:
invoke a directive by using: Element; Attribute; Class; Comment;

添加限制范圍:
can restrict your directives to only be invoked by some of the methods.

 /* 
  E for Element name
  A for Attribute
  C for Class
  M for Comment
*/

return { restrict : "EA",
    template : "<h1>Made by a directive!</h1>"
};

AngularJS ng-model Directive

The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

  1. Validate User Input
<form ng-app="" name="myForm">
    Email:
    <input type="email" name="myAddress" ng-model="text">
    <span ng-show="myForm.myAddress.$error.email">Not a valid e-mail address</span>
</form>
  1. Application Status
    The ng-model directive can provide status for application data (invalid, dirty, touched, error):
<form ng-app="" name="myForm" ng-init="myText = 'post@myweb.com'">
    Email:
    <input type="email" name="myAddress" ng-model="myText" required>
    <h1>Status</h1>
    {{myForm.myAddress.$valid}}
    {{myForm.myAddress.$dirty}}
    {{myForm.myAddress.$touched}}
</form>
  1. CSS Classes
    The ng-model directive provides CSS classes for HTML elements, depending on their status:
<style>
input.ng-invalid {
    background-color: lightblue;
}
</style>
<body>
<form ng-app="" name="myForm">
    Enter your name:
    <input name="myName" ng-model="myText" required>
</form>
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/1643592-36b4782146a0e4f1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

The ng-model directive adds/removes the following classes, according to the status of the form field:
ng-empty
ng-not-empty
ng-touched
ng-untouched
ng-valid
ng-invalid
ng-dirty
ng-pending
ng-pristine

數據綁定

什么是 Data Model

AngularJS applications usually have a data model. The data model is a collection of data available for the application.

什么是 HTML View

The HTML container where the AngularJS application is displayed, is called the view.

什么是 Two-way Binding

Data binding in AngularJS is the synchronization between the model and the view.

When data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well. This happens immediately and automatically, which makes sure that the model and the view is updated at all times.

AngularJS Controllers

AngularJS controllers control the data of AngularJS applications.

A controller is a JavaScript Object, created by a standard JavaScript object constructor.

應用執(zhí)行流程

AngularJS應用程序由ng-app =“myApp”定義功戚。 應用程序在<div>中運行溉瓶。

ng-controller =“myCtrl”屬性是一個AngularJS指令扩氢。 它定義了一個控制器哮独。

The myCtrl function is a JavaScript function.

AngularJS will invoke the controller with a $scope object.
AngularJS將使用$ scope對象調用控制器。

In AngularJS, $scope is the application object (the owner of application variables and functions).
在AngularJS中疮胖,$ scope是應用程序對象(應用程序變量和函數的所有者)俯萎。

The controller creates two properties (variables) in the scope (firstName and lastName).

The ng-model directives bind the input fields to the controller properties (firstName and lastName).
ng-model指令將輸入字段綁定到控制器屬性(firstName和lastName)。

AngularJS Scope

The scope is the binding part between the HTML (view) and the JavaScript (controller).

The scope is an object with the available properties and methods.

The scope is available for both the view and the controller.

Understanding the Scope

If we consider an AngularJS application to consist of:

  • View, which is the HTML.
  • Model, which is the data available for the current view.
  • Controller, which is the JavaScript function that makes/changes/removes/controls the data.
    Then the scope is the Model.

The scope is a JavaScript object with properties and methods, which are available for both the view and the controller.

AngularJS Filters

Filters can be added in AngularJS to format data.

** Filters can be added to expressions by using the pipe character |, followed by a filter. **

Paste_Image.png
<p>The name is {{ lastName | lowercase }}</p>

// The orderBy filter sorts an array:
<li ng-repeat="x in names | orderBy:'country'">

// Return the names that contains the letter "i":
<li ng-repeat="x in names | filter : 'i'">

Custom Filters

<ul ng-app="myApp" ng-controller="namesCtrl">
    <li ng-repeat="x in names">
        {{x | myFormat}}
    </li>
</ul>

<script>
var app = angular.module('myApp', []);
app.filter('myFormat', function() {
    return function(x) {
        var i, c, txt = "";
        for (i = 0; i < x.length; i++) {
            c = x[i];
            if (i % 2 == 0) {
                c = c.toUpperCase();
            }
            txt += c;
        }
        return txt;
    };
});
app.controller('namesCtrl', function($scope) {
    $scope.names = ['Jani', 'Carl', 'Margareth', 'Hege', 'Joe', 'Gustav', 'Birgit', 'Mary', 'Kai'];
});
</script>

AngularJS Services

In AngularJS, a service is a function, or object, that is available for, and limited to, your AngularJS application.

AngularJS has about 30 built-in services. One of them is the $location service.

The $location service has methods which return information about the location of the current web page:

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $location) {
    $scope.myUrl = $location.absUrl();
});

built-in services;

$location; $http; $setTimeout; $setInterval; ...

Create Your Own Service

app.service('hexafy', function() {
    this.myFunc = function (x) {
        return x.toString(16);
    }
});

app.controller('myCtrl', function($scope, hexafy) {
    $scope.hex = hexafy.myFunc(255);
});

** Once you have created a service, and connected it to your application, you can use the service in any controller, directive, filter, or even inside other services.**

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末芥吟,一起剝皮案震驚了整個濱河市侦铜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌钟鸵,老刑警劉巖钉稍,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異棺耍,居然都是意外死亡贡未,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來俊卤,“玉大人嫩挤,你說我怎么就攤上這事∠校” “怎么了岂昭?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長狠怨。 經常有香客問我约啊,道長,這世上最難降的妖魔是什么佣赖? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任恰矩,我火速辦了婚禮,結果婚禮上憎蛤,老公的妹妹穿的比我還像新娘外傅。我一直安慰自己,他們只是感情好蹂午,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布栏豺。 她就那樣靜靜地躺著,像睡著了一般豆胸。 火紅的嫁衣襯著肌膚如雪奥洼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天晚胡,我揣著相機與錄音灵奖,去河邊找鬼。 笑死估盘,一個胖子當著我的面吹牛瓷患,可吹牛的內容都是我干的。 我是一名探鬼主播遣妥,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼擅编,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了箫踩?” 一聲冷哼從身側響起爱态,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎境钟,沒想到半個月后锦担,有當地人在樹林里發(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡慨削,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年洞渔,在試婚紗的時候發(fā)現(xiàn)自己被綠了套媚。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡磁椒,死狀恐怖堤瘤,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情衷快,我是刑警寧澤宙橱,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布姨俩,位于F島的核電站蘸拔,受9級特大地震影響,放射性物質發(fā)生泄漏环葵。R本人自食惡果不足惜调窍,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望张遭。 院中可真熱鬧邓萨,春花似錦、人聲如沸菊卷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽洁闰。三九已至歉甚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間扑眉,已是汗流浹背纸泄。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留腰素,地道東北人聘裁。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像弓千,于是被迫代替她去往敵國和親衡便。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內容