AngularJS 指令

AngerlarJS指令的概念:

      1.通过'指令'对HTML进行扩展,前缀为‘ng-’

      2.通过内置指令添加新功能

      3.运行自定义指令

常见指令的应用:

     初始化程序为angerlarJS程序: ng-app

     初始化数据: ng-init

     绑定实体或数据: ng-model

 

注意:

一个网页可以包含多个AngerlarJS程序

AngularJS 完美支持数据库的 CRUD(增加Create、读取Read、更新Update、删除Delete)应用程序。
 

重复指令:ng-repeat

案例:

 循环一个json对象数组
        <div ng-init="shuzu1=[{name:'licong',age:'18'},{name:'zhangsan',age:'19'}]">
            <table border="1">
                <tr><th>姓名</th><th>年龄</th></tr>
                <tr ng-repeat="x in shuzu1"><td>{{x.name}}</td><td>{{x.age}}</td></tr>
            </table>
        </div>

 

 

ng-app 指令

ng-app 指令定义了 AngularJS 应用程序的 根元素

ng-app 指令在网页加载完毕时会自动引导(自动初始化)应用程序。

ng-app 指令标记了AngerlarJS脚本的作用域,在<html>中添加ng-app属性即说明整个<html>都是angerlarjs脚本作用域。开发者也可以在局部使用ng-app指令,如<div ng-app>,则Angerlarjs脚本仅在该<div>中运行。

 

ng-init 指令

ng-init 指令为 AngularJS 应用程序定义了 初始值

通常情况下,不使用 ng-init。您将使用一个控制器或模块来代替它。

 

ng-model 指令

ng-model 指令 绑定 HTML 元素 到应用程序数据。

ng-model 指令也可以:

  • 为应用程序数据提供类型验证(number、email、required)。
  • 为应用程序数据提供状态(invalid、dirty、touched、error)。
  • 为 HTML 元素提供 CSS 类。
  • 绑定 HTML 元素到 HTML 表单。

 

ng-repeat 指令

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

 

创建自定义的指令

使用 .directive 函数来添加自定义的指令

要调用自定义指令,HTML 元素上需要添加自定义指令名

使用驼峰法来命名一个指令, runoobDirective, 但在使用它时需要以 - 分割, runoob-directive:

 

可以通过以下方式来调用自定义指令

  • 元素名
  • 属性
  • <div ng-app="MyDirectives">
            <h2>自定义指令</h2>
            1.元素名访问自定义指令
            <run-Directives></run-Directives>
    
            2.属性
            <div run-Directives></div>
    
            <script>
                var app = angular.module("MyDirectives", []);
                app.directive("runDirectives", function () {
                    return {
    
                        template: "<h2>我的自定义指令</h2>"
                    };
                });
            </script>
        </div>
    

     

  • 类名 (必须设置 restrict 的值为 "C" 才能通过类名来调用指令)
  • <div ng-app="MySecondDrictives">
            3.类
            <div class="second-Drrective"></div>
    
             <script>
                var app = angular.module("MySecondDrictives", []);
                app.directive("secondDrrective", function () {
                    return {
                        restrict: "C",
                        template: "<h1>使用类访问自定义指令</h1>"
                    };
                });
            </script>
        </div>
    

     

  • 注释 (格式很重要: <!-- directive: 指令名称  -->中间必须有间隔,directive不能打错)
  •  <div ng-app="MyThirdDrictives">
            4.注释
            <!-- directive: zhushi-dirctives -->
            <script>
                var app1 = angular.module("MyThirdDrictives", []);
                app1.directive("zhushiDirctives", function () {
                    return {
                        restrict: "M",
                        replace: true,
                        template: "<h1>使用注释访问自定义指令</h1>"
                    };
                });
            </script>
        </div>
    

 

限制使用

可以限制你的指令只能通过特定的方式来调用。

restrict 值可以是以下几种:

  • E 作为元素名使用
  • A 作为属性使用
  • C 作为类名使用
  • M 作为注释使用

restrict 默认值为 EA, 即可以通过元素名和属性名来调用指令。

 

posted @ 2016-11-28 15:47  微笑代表淡定.Net  阅读(123)  评论(0)    收藏  举报