莫菲    

AngularJS学习笔记--(一)

7年前发布  · 968 次阅读
  AngularJs 

经常被jquery js整的想疯,决定学下AngularJS,以后尽量不要被整那么惨 (๑• . •๑)

AngularJS学习资料(不断完善):

AngularJS菜鸟教程

简单例子:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="">
 	<p>名字 : <input type="text" ng-model="name"></p>
 	<h1>Hello {{name}}</h1>
</div>

</body>
</html>
基本指令:

当网页加载完毕,AngularJS 自动开启。
ng-app 指令告诉 AngularJS,<div> 元素是 AngularJS 应用程序 的"所有者"。
ng-model 指令把输入域的值绑定到应用程序变量 name。
ng-bind 指令把应用程序变量 name 绑定到某个段落的 innerHTML。
ng-init 指令初始化 AngularJS 应用程序变量
ng-repeat 指令会重复一个 HTML 元素:

指令部分详解:

ng-model 指令:
ng-model 指令 绑定 HTML 元素 到应用程序数据。
ng-model 指令也可以:
为应用程序数据提供类型验证(number、email、required)。
为应用程序数据提供状态(invalid、dirty、touched、error)。
为 HTML 元素提供 CSS 类。
绑定 HTML 元素到 HTML 表单。

ng-repeat 指令对于集合中(数组中)的每个项会 克隆一次 HTML 元素

AngularJS 表达式:

AngularJS 表达式写在双大括号内:{{ expression }}。例如{{1+2}}

 

AngularJS 应用:

AngularJS 模块(Module) 定义了 AngularJS 应用。

AngularJS 控制器(Controller) 用于控制 AngularJS 应用。

ng-app指令定义了应用, ng-controller 定义了控制器。

简单实例:

<!doctype html>
<html lang="ch-zn">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="js/angular.min.js"></script>
    <script src="index.js"></script>
</head>
<body>
<p>尝试修改以下表单。</p>
<div ng-app="myApp">
    <div  ng-controller="myCtrl">
        名: <input type="text" ng-model="firstName"><br>
        姓: <input type="text" ng-model="lastName"><br>
        <br>
        姓名: {{firstName + " " + lastName}}
    </div>
    <div ng-init="quantity=1;cost=5">
        <p>总价: <span ng-bind="quantity * cost"></span></p>
    </div>
    <div ng-init="firstName='mo';lastName='fei'">
        <p>姓名: {{ firstName + " " + lastName }}</p>
    </div>
<!--    <div ng-app="" ng-init="firstName='mo';lastName='fei'">
        <p>姓名: <span ng-bind="firstName + ' ' + lastName"></span></p>
    </div>-->
    <div ng-init="person={firstName:'mo',lastName:'fei'}">
        <p>姓为 {{ person.lastName }}</p>
    </div>
<!--    <div ng-app="" ng-init="person={firstName:'mo',lastName:'fei'}">
        <p>姓为 <span ng-bind="person.lastName"></span></p>
    </div>-->
    <div ng-init="points=[1,15,19,2,40]">
        <p>第三个值为 {{ points[2] }}</p>
    </div>
    <div ng-init="quantity=1;price=5">
        <h2>价格计算器</h2>
        数量: <input type="number"	ng-model="quantity">
        价格: <input type="number" ng-model="price">
        <p><b>总价:</b> {{ quantity * price }}</p>
    </div>
    <div ng-init="boys=['Jani','Hege','Kai']">
        <p>使用 ng-repeat 来循环数组</p>
        <ul>
            <li ng-repeat="x in boys">
                {{ x }}
            </li>
        </ul>
    </div>
    <div ng-init="names=[
    {name:'Jani',country:'Norway'},
    {name:'Hege',country:'Sweden'},
    {name:'Kai',country:'Denmark'}]">
        <p>循环对象:</p>
        <ul>
            <li ng-repeat="x in names">
                {{ x.name + ', ' + x.country }}
            </li>
        </ul>
    </div>
    <runoob-directive></runoob-directive>
</div>
</body>
</html>
/**
 * Created by user on 2016/5/28.
 */
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName= "mo";
    $scope.lastName= "fei";
});
app.directive("runoobDirective", function() {
    return {
        template : "<h1>自定义指令!</h1>"
    };
});