AngularJS 使用元素与事件指令
来源:互联网 发布:淘宝网首页百色针织 编辑:程序博客网 时间:2024/06/06 20:13
学习要点:
- 使用元素指令
- 显示、隐藏和移除元素
- 管理类和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 exampleAppangular.module("exampleApp", []) // define a controller named defaultCtrl .controller('defaultCtrl', function ($scope) { $scope.todos = [ { action : 'play ball', complete : false }, { action : 'runnging', complete : false }, { 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 exampleAppangular.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 }, { action : 'runnging', complete : false }, { action : 'eating', complete : true }, { action : 'shopping', 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 exampleAppangular.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 }, { action : 'runnging', complete : false }, { action : 'eating', complete : true }, { action : 'shopping', complete : false } ]; $scope.buttonNames = ["Red", "Green", "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 exampleAppangular.module("exampleApp", []) // define a controller named defaultCtrl .controller('defaultCtrl', 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 exampleAppangular.module("exampleApp", []) // define a controller named defaultCtrl .controller('defaultCtrl', 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>
0 0
- AngularJS 使用元素与事件指令
- angularjs事件指令
- angularJS 事件指令
- Angularjs中使用指令绑定点击事件的方法
- AngularJs ng-change事件/指令
- 六、AngularJS 之事件指令
- angularjs指令的使用
- AngularJS学习记录-指令复制元素
- angularJS 常用的渲染指令、事件指令和节点指令
- angularjs学习笔记—事件指令
- angularjs学习笔记—事件指令
- AngularJS学习笔记—事件指令
- angularjs学习笔记—事件指令
- angularjs学习笔记—事件指令
- angularjs 的指令 和 事件 的小结
- AngularJs指令中使用controller
- 指令与脚本元素
- angularjs学习之六(angularjs中directive指令的一般编程事件绑定 模板使用等)
- 魅族MX5data目录下不显示
- scrapy爬虫框架学习入门教程及实例
- window.load和window.onload
- android studio导入xutils编译时报错:找不到HttpRequestBase
- FFMPEG智能缓冲实现及应用
- AngularJS 使用元素与事件指令
- 表单+servlet 网络调查表
- 从Python中readline()函数读取的一行内容中去掉换行符\n
- C++第四次作业
- 通过指向结构体变量的指针变量输出结构体变量中成员的信息
- Java泛型-类型擦除
- 折半插入排序
- uva11020 Efficient Solutions
- Yarn公平调度器之DRF算法