学习要点:

  • 使用元素指令
    • 显示、隐藏和移除元素
    • 管理类和CSS
  • 处理事件
  • 管理特殊属性

背景代码

<!DOCTYPE> <!-- use module --> <html ng-app="exampleApp"> <head> <title>Angular Directive</title> <Meta charset="utf-8"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"> </head> <body> <div id="todoPanel" class="panel" ng-controller="defaultCtrl"> <h3 class="panel-header">To Do List</h3> <table class="table table-striped table-bordered table-hover"> <tr><th>#</th><th>Aciton</th><th>Done</th><th>Status</th></tr> <tr ng-repeat="item in todos"> <td>{{$index + 1}}</td> <td ng-repeat="prop in item">{{prop}}</td> </tr> </table> </div> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript"> // define a module named exampleApp angular.module("exampleApp",[]) // define a controller named defaultCtrl .controller('defaultCtrl',function ($scope) { $scope.todos = [ { action : 'play ball',complete : false },{ action : 'runnging',{ action : 'eating',complete : true },{ action : 'shopping',complete : false } ]; }) </script> </body> </html>

一、使用元素指令
1.显示、隐藏和移除元素

<div class="checkBox well"> <label> <input type="checkBox" ng-model="todos[2].complete" />Item 3 is complete </label> </div> <table class="table table-striped table-bordered table-hover"> <tr><th>#</th><th>Aciton</th><th>Done</th><th>Status</th></tr> <tr ng-repeat="item in todos"> <td>{{$index + 1}}</td> <td ng-repeat="prop in item">{{prop}}</td> <td> <!-- <span ng-hide="item.complete"> (Imcomplete)</span> <span ng-show="item.complete"> (complete)</span> --> <span ng-if="!item.complete"> (Imcomplete)</span> <span ng-if="item.complete"> (complete)</span> </td> </tr> </table>

解决表单的条纹化问题以及ng-repeat的冲突—过滤器

<div class="checkBox well"> <label> <input type="checkBox" ng-model="todos[2].complete" />Item 3 is complete </label> </div> <table class="table table-striped table-bordered table-hover"> <tr><th>#</th><th>Aciton</th><th>Done</th></tr> <tr ng-repeat="item in todos | filter : { complete :'false' }"> <td>{{$index + 1}}</td> <td ng-repeat="prop in item">{{prop}}</td> </tr> </table>

2.管理类和CSS
单击行列的颜色按钮,更换表格的行列颜色

// define a module named exampleApp
angular.module("exampleApp",[])
    // define a controller named defaultCtrl
    .controller('defaultCtrl',function ($scope) {
        // $scope.message = "Tap Me";
        // $scope.dataValue = false;
        $scope.todos = [
            { action : 'play ball',complete : false },complete : true },complete : false }
        ];
        // 在controller中添加内容
        $scope.buttonNames = ["Red","Green","Blue"];
        $scope.settings = {
            Rows : "Red",Columns : "Green"
        };
    })
<style> .Red { background-color: lightcoral; } .Green { background-color: lightgreen; } .Blue { background-color: lightblue; } </style>
<div class="row well"> <div class="col-xs-6" ng-repeat="(key,val) in settings"> <h4>{{key}}</h4> <div class="radio" ng-repeat="button in buttonNames"> <label> <input type="radio" ng-model="settings[key]" value="{{button}}">{{button}} </label> </div> </div> </div> <table class="table table-striped table-bordered table-hover"> <tr><th>#</th><th>Aciton</th><th>Done</th></tr> <tr ng-repeat="item in todos" ng-class="settings.Rows"> <td>{{$index + 1}}</td> <td>{{item.action}}</td> <td ng-style="{'background-color' : settings.Columns }"> {{item.complete}} </td> </tr> </table>

设置奇数行和偶数行的CSS样式

<table class="table table-striped table-bordered"> <tr><th>#</th><th>Aciton</th><th>Done</th></tr> <tr ng-repeat="item in todos" ng-class-even="settings.Rows" ng-class-odd="settings.Columns"> <td>{{$index + 1}}</td> <td>{{item.action}}</td> <td>{{item.complete}}</td> </tr> </table>

二、事件处理
1.单击不同的按钮切换表格颜色
样式部分

<style> .Red { background-color: lightcoral; } .Green { background-color: lightgreen; } .Blue { background-color: lightblue; } </style>

JS部分

// define a module named exampleApp
angular.module("exampleApp",complete : false }
        ];
        $scope.buttonNames = ["Red","Blue"];
        $scope.data = {
            rowColor : "Blue",columnColor : "Green"
        };
        $scope.handleEvent = function (e) {
            console.log("Event type: " + e.type);
            $scope.data.columnColor = e.type == "mouSEOver" ? "Green" : "Blue";
        }
    })

视图部分

<div class="well"> <span ng-repeat="button in buttonNames"> <button class="btn btn-info" ng-click="data.rowColor = button">{{button}}</button> </span> </div> <table class="table table-striped table-bordered"> <tr><th>#</th><th>Aciton</th><th>Done</th></tr> <tr ng-repeat="item in todos" ng-class="data.rowColor" ng-mouseenter="handleEvent($event)" ng-mouseleave="handleEvent($event)"> <td>{{$index + 1}}</td> <td>{{item.action}}</td> <td ng-class="data.columnColor">{{item.complete}}</td> </tr> </table>

2.自定义指令
脚本

// define a module named exampleApp
angular.module("exampleApp",function ($scope) {
        $scope.message = "Tap Me";
    })
    .directive("tap",function () {
        return function (scope,elem,attrs) {
            elem.on("click",function () {
                scope.$apply(attrs["tap"]);
            })
        }
    })

视图部分

<div class="well" tap="message = 'Tapped!'"> {{message}} </div>

三、管理特殊属性
1.布尔属性

// define a module named exampleApp
angular.module("exampleApp",function ($scope) {
        $scope.dataValue = false;
    });

视图

<div class="well checkBox">
    <label>
        <input type="checkBox" ng-model="dataValue">禁用按钮
    </label>
</div>
<button class="btn btn-success" ng-disabled="dataValue">按钮</button>

AngularJS 使用元素与事件指令的更多相关文章

  1. Swift之UIColor 扩展

    通常UIColor自带的一些方法在实际项目开发中不能满足我们的需求,所以把一些常用到的方法在这里进行一个归类在实际项目中还有很多,今天就写两个,等总结多几个再添加上去未完待续

  2. 正则表达式处理XML

    若想取标记之间的内容,可以这样分析表达式说明正则取xml内容比dom4j快50倍?

  3. Ajax / Jquery自动完成JSON数据

    我正在设置我的JqueryUI自动填充字段以从ajax连接获取数据.这是我的代码到目前为止这是我的JSON:HTML:当我开始输入“mil”我的代码给我这个错误:编辑:我做了你的改变,这有几个尝试,但现在我得到一个新的错误–[URL]第55行第25列处理未处理的例外0x800a1391–MicrosoftJScript运行时错误:’data’未定义您需要将成功回调更改为Fiddle.jQuery.map有助于将数组或对象中的所有项目转换为新的项目数组.更新:添加过滤器

  4. ajax请求到后台数据,前台不用拼接字符串append追加HTML标签,一样显示到页面 使用空模板

    --表格模板---用于处理获取后台数据后生成表格,-->aabbccddffee

  5. ajax请求得到后台数据,前台页面,使用table模板然后clone,显示表格,,不用append拼接 “字符串和HTML表格标签”

    --表格模板---用于处理获取后台数据后生成表格,-->aabbccddffee张宇:你好!

  6. AJAX+SpringMVC 获取后台数据的方式

    利用ajax同步获取服务器的数据,当页面加载完成的时候后台代码:Service层:List转json方法:学习中。。。。。。

  7. $.ajax() 和$.each()一个使用实例

    一、$.ajax()二、$.each()1.对象的遍历2.标签元素的遍历

  8. ajax实现网页的局部刷新

    从页面到ajax函数的运行的大致思路:页面点击事件,触发xmlHttp.open函数的执行。通过web.xml的配置的servlet-mapping,url所附带的参数会被处理的servlet类接收,此类中可进行数据库的查询,并将查询结果以xml文件的格式保存下来,页面会以解析xml文件将值传到前台,完成了局部刷新的功能。

  9. angularJs的国家选择控制

    p=preview使用ng-option指令使用ng-repeat指令:控制器中的国家范围:

  10. AngularJS在自定义指令中包含ui-select

    我设法通过在模板中设置ng-model来进行绑定工作

随机推荐

  1. Angular2 innerHtml删除样式

    我正在使用innerHtml并在我的cms中设置html,响应似乎没问题,如果我这样打印:{{poi.content}}它给了我正确的内容:``但是当我使用[innerHtml]=“poi.content”时,它会给我这个html:当我使用[innerHtml]时,有谁知道为什么它会剥离我的样式Angular2清理动态添加的HTML,样式,……

  2. 为Angular根组件/模块指定@Input()参数

    我有3个根组件,由根AppModule引导.你如何为其中一个组件指定@input()参数?也不由AppModalComponent获取:它是未定义的.据我所知,你不能将@input()传递给bootstraped组件.但您可以使用其他方法来做到这一点–将值作为属性传递.index.html:app.component.ts:

  3. angular-ui-bootstrap – 如何为angular ui-bootstrap tabs指令指定href参数

    我正在使用角度ui-bootstrap库,但我不知道如何为每个选项卡指定自定义href.在角度ui-bootstrap文档中,指定了一个可选参数select(),但我不知道如何使用它来自定义每个选项卡的链接另一种重新定义问题的方法是如何使用带有角度ui-bootstrap选项卡的路由我希望现在还不算太晚,但我今天遇到了同样的问题.你可以通过以下方式实现:1)在控制器中定义选项卡href:2)声明一个函数来改变控制器中的散列:3)使用以下标记:我不确定这是否是最好的方法,我很乐意听取别人的意见.

  4. 离子框架 – 标签内部的ng-click不起作用

    >为什么标签标签内的按钮不起作用?>但是标签外的按钮(登陆)工作正常,为什么?>请帮我解决这个问题.我需要在点击时做出回复按钮workingdemo解决方案就是不要为物品使用标签.而只是使用divHTML

  5. Angular 2:将值传递给路由数据解析

    我正在尝试编写一个DataResolver服务,允许Angular2路由器在初始化组件之前预加载数据.解析器需要调用不同的API端点来获取适合于正在加载的路由的数据.我正在构建一个通用解析器,而不是为我的许多组件中的每个组件设置一个解析器.因此,我想在路由定义中传递指向正确端点的自定义输入.例如,考虑以下路线:app.routes.ts在第一个实例中,解析器需要调用/path/to/resourc

  6. angularjs – 解释ngModel管道,解析器,格式化程序,viewChangeListeners和$watchers的顺序

    换句话说:如果在模型更新之前触发了“ng-change”,我可以理解,但是我很难理解在更新模型之后以及在完成填充更改之前触发函数绑定属性.如果您读到这里:祝贺并感谢您的耐心等待!

  7. 角度5模板形式检测形式有效性状态的变化

    为了拥有一个可以监听其包含的表单的有效性状态的变化的组件并执行某些组件的方法,是reactiveforms的方法吗?

  8. Angular 2 CSV文件下载

    我在springboot应用程序中有我的后端,从那里我返回一个.csv文件WheniamhittingtheURLinbrowsercsvfileisgettingdownloaded.现在我试图从我的角度2应用程序中点击此URL,代码是这样的:零件:服务:我正在下载文件,但它像ActuallyitshouldbeBook.csv请指导我缺少的东西.有一种解决方法,但您需要创建一个页面上的元

  9. angularjs – Angular UI-Grid:过滤后如何获取总项数

    提前致谢:)你应该避免使用jQuery并与API进行交互.首先需要在网格创建事件中保存对API的引用.您应该已经知道总行数.您可以使用以下命令获取可见/已过滤行数:要么您可以使用以下命令获取所选行的数量:

  10. angularjs – 迁移gulp进程以包含typescript

    或者我应该使用tsc作为我的主要构建工具,让它解决依赖关系,创建映射文件并制作捆绑包?

返回
顶部