AngularJS(1)——AngularJS指令机制
About
AngularJS Directives
<!DOCTYPE html>
<html><body><div ng-app="" ng-init="learn='Angular'"><p>Input something you want to learn:<input type="text" ng-model="learn"></p>
<p>You wrote: {{ learn}}</p></div></body><script src="js/angular-1.3.0.js"></script>
</html>
Two-Way Binding
Validate User Input
<!DOCTYPE html>
<html><body><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></body><script src="js/angular-1.3.0.js"></script>
</html>
Application Status
<!DOCTYPE html>
<html><body><form ng-app="" name="myForm" ng-init="myText = 'post@myweb.com'">Email:
<input type="email" name="myAddress" ng-model="myText" required>
<p>Edit the e-mail address, and try to change the status.</p>
<h1>Status</h1>
<p>Valid: {{myForm.myAddress.$valid}} (if true, the value meets all criteria).</p>
<p>Dirty: {{myForm.myAddress.$dirty}} (if true, the value has been changed).</p>
<p>Touched: {{myForm.myAddress.$touched}} (if true, the field has been in focus).</p></form></body><script src="js/angular-1.3.0.js"></script>
</html>
CSS Classes
<!DOCTYPE html>
<html><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><p>Edit the text field and it will get/lose classes according to the status.</p>
<p><b>Note:</b> A text field with the "required" attribute is not valid when it is empty.</p></body><script src="js/angular-1.3.0.js"></script>
</html>
Create New Directives
restrict | 意义 |
E | Element name |
A | Attribute |
C | Class |
M | Comment |
<!DOCTYPE html>
<html ng-app="MyModule">
<head><meta charset="utf-8">
</head>
<body><hello></hello><div hello></div><div class="hello"></div>
</body><script src="js/angular-1.3.0.js"></script><script src="HelloAngular_Directive.js"></script>
</html>JS文件:
var MyModule=angular.module("MyModule",[]);
MyModule.directive("hello", function () {return {restrict: 'ACME',template: '<div>Hi everyone!</div>',replace: true}
});
Expand
js文件:
var expModule=angular.module('expanderModule',[])
expModule.directive('accordion', function() {return {restrict : 'EA',replace : true,transclude : true,template : '<div ng-transclude></div>',controller : function() {var expanders = [];this.gotOpened = function(selectedExpander) {angular.forEach(expanders, function(expander) {if (selectedExpander != expander) {expander.showMe = false;}});}this.addExpander = function(expander) {expanders.push(expander);}}}
});expModule.directive('expander', function() {return {restrict : 'EA',replace : true,transclude : true,require : '^?accordion',scope : {title : '=expanderTitle'},template : '<div>'+ '<div class="title" ng-click="toggle()">{{title}}</div>'+ '<div class="body" ng-show="showMe" ng-transclude></div>'+ '</div>',link : function(scope, element, attrs, accordionController) {scope.showMe = false;accordionController.addExpander(scope);scope.toggle = function toggle() {scope.showMe = !scope.showMe;accordionController.gotOpened(scope);}}}
});expModule.controller("SomeController",function($scope) {$scope.expanders = [{title : 'Click me to expand',text : 'Hi there folks, I am the content that was hidden but is now shown.'}, {title : 'Click this',text : 'I am even better text than you have seen previously'}, {title : 'Test',text : 'test'},{title:'hello world',text:'this is create by me'}];
});html文件:
<html ng-app="expanderModule"><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><script src="js/angular-1.3.0.js"></script><link rel="stylesheet" type="text/css" href="Accordion.css"/></head><body ng-controller='SomeController' ><accordion><expander class='expander' ng-repeat='expander in expanders' expander-title='expander.title'>{{expander.text}}</expander></accordion></body><script src="Accordion.js"></script>
</html>
总结
AngularJS(1)——AngularJS指令机制相关推荐
- AngularJS 作用域与数据绑定机制
AngularJS 简介 AngularJS 是由 Google 发起的一款开源的前端 MVC 脚本框架,既适合做普通 WEB 应用也可以做 SPA(单页面应用,所有的用户操作都在一个页面中完成).与 ...
- AngularJS中的指令全面解析(必看)
出处: http://www.jb51.net/article/84665.htm 说到AngularJS,我们首先想到的大概也就是双向数据绑定和指令系统了,这两者也是AngularJS中最为吸引人的 ...
- 带你走近AngularJS - 创建自定义指令
带你走近AngularJS系列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自定义指令 ------------- ...
- AngularJS 的自定义指令
这是一篇angularjs自定义指令的教程.就让我们来看看,如何在angularjs中自定义指令吧!我们将利用现有的guitar angularjs app,这在 教程14: AngularJ表单验证 ...
- 使用Angularjs的ng-cloak指令避免页面乱码
在使用Anguarjs进行web开发或者进行SPA(single page application)开发时,往往会遇到下面这样的问题. 刷新页面时,页面会出现一些乱码,这里的乱码具体是指`{{expr ...
- angularjs笔记,基本指令,字符串反转,过滤器
1. Angular指令 * Angular为HTML页面扩展的: 自定义标签属性或标签 * 与Angular的作用域对象(scope)交互,扩展页面的动态表现力 2. 常用指令(一) ...
- 关于Angularjs中自定义指令一些有价值的细节和技巧
作者:心叶 时间:2018-04-22 10:58 一:自定义指令常用模板 下面是大致的说明,不是全面的,后面来具体说明一些没有提及的细节和重要的相关知识: angular.module('yello ...
- AngularJS内置指令
指令,我将其理解为AngularJS操作HTML element的一种途径. 由于学习AngularJS的第一步就是写内置指令ng-app以指出该节点是应用的根节点,所以指令早已不陌生. 这篇日志简 ...
- angularJS在创建指令需要注意的问题(指令中使用ngRepeat)
现在发现,当初的自己真的是太菜了,为什么你在指令中更改数据,没有作用呢?这其实是原型链的问题. 详细的我就不在这里说了,有位大神早已发布了这个内容,在这里复制个地址给大家,有兴趣的可以看看 http: ...
最新文章
- Global.asax中Application_Error无法执行
- Science子刊带来新遗传证据:早期人类驯化了自己
- windows快捷启动命令
- MATLAB从入门到精通-APP调用simulink中的参数,并且修改,将结果返回到APP中
- Python入门100题 | 第058题
- mysql制作html静态网页6_将数据库中的所有内容生成html静态页面的代码
- 腾讯AI Lab副主任俞栋:语音识别领域的现状与进展
- Java 7中对String.substring的更改
- JS键盘字母相应的keyCode值
- *** cannot be resolved or is not a field
- ghost mysql 乱码,ghost安装之mysql
- struts教程笔记6
- python查看帮助文档:help(命令)
- Wilcoxon符号秩+秩和检验学习[转载]
- 【NLP】keras实现IMDB电影评论分类
- 谈谈写博客的意义和感受
- 公众号里面套页面_怎么套用别人的公众号模板?公众号模板在哪找? | 微信公众号指南...
- 数字接龙 用计算机完成318,微信报名接龙数字如何排列对齐传递
- minikube国内安装之曲线救国
- 网络 路由器基本协议配置