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.

应用执行流程

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.**

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容