angular总结-directive指令基础
总结自http://www.imooc.com/learn/156 一,指令基础概念1,第一个指令<my-directive></my-directive>
假设我们已经创建了一个完整的HTML文档,其中包含了AngularJS,并且DOM中已经用ng-app指令标识出了应用的根元素,当AngularJS编译HTML时就会调用指令。myDirective指令的定义看起来是这样的: angular.module('myApp',[])
.directive('myDirective',function() {
return {
restrict: 'E',template: '<a href="http://google.com">Click me to go to Google</a>'
};
});
2,基础概念
二,restrict—匹配模式1,基础概念
2,示例<!doctype html>
<html ng-app="MyModule">
<head>
<meta charset="utf-8">
</head>
<body>
<hello></hello> <!--元素E-->
<div hello></div> <!--属性A-->
<div class="hello"></div> <!--样式C-->
<!-- directive:hello --> <!--注释M-->
<div></div>
</body>
<script src="lib/angular/angular.min.js"></script>
<script src="Directive_Hello.js"></script>
</html>
var myModule = angular.module("MyModule",[]);
myModule.directive("hello",function () {
return {
restrict: 'AEMC',template: '<div>hi everyone!</div>',replace: true
}
});
三,template—模版引入1,template(字符串或函数)template参数是可选的,必须被设置为以下两种形式之一:
2,templateUrl(字符串或函数)templateUrl是可选的参数,可以是以下类型:
模板加载后,AngularJS会将它默认缓存到$templateCache服务中! 3,$templateCachevar myModule = angular.module("MyModule",[]);
//注射器加载完所有模块时,此方法执行一次
myModule.run(function ($templateCache) {
$templateCache.put("hello.html","<div>hi everyone!</div>");
});
myModule.directive("hello",function ($templateCache) {
return {
restrict: 'AEMC',template: $templateCache.get("hello.html"),replace: true
}
});
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |