首页 技术 正文
技术 2022年11月10日
0 收藏 310 点赞 3,930 浏览 2479 个字

目录:directive()   restrict   replace   template   templateUrl  scope  transclude   ng-transclude   controller

directive()

自定义指令,第一个参数是自定义指令的名字,第二个参数是一个函数,这个函数的返回值就是自定义指令该干的事情。

 //自定义指令指定控制器的名字
.directive("myDirective",function(){
return{ }
})

restrict

指定调用自定义指令的方式:A属性/E标签/C类/M注释。推荐用属性的形式调用自定义指令。

.directive("myDirective",function(){
return{
//指定此指令用属性调用
restrict:'A' }
})

replace

规定自定义指令模板是被包含在指令元素里面还是替换自定义指令,true表示模板替换视图中的自定义指令。false是默认值,表示模板被嵌套在自定义指令中。

.directive("myDirective",function(){
return{ //规定调用指令的方式
restrict:'A', //模板替换自定义指令
replace:true, //定义模板
template:'<a href="http://www.baidu.com" rel="external nofollow" >点我跳到百度<span>{{my}}</span></a>'
}
})

template

定义模板,值是一个html元素的字符串。参考replace代码

templateUrl

同template相似,值是一个引入外部模板的路径,不能跨域。

.directive("myDirective",function(){
return{ //规定调用指令的方式
restrict:'A', //模板替换视图中的指令
replace:true, //定义模板路径
templateUrl:'../tem.html'
}
})

scope

创建隔离作用域,值为true时会继承父级作用域,创建一个新的作用域,值设置为一个对象时,不会继承父级作用域。使用@符号指定与视图中的数据的绑定。

transclude   ng-transclude

值为true时表示将页面自定义指令中的子元素添加到模板里带有 ng-transclude 属性的元素的下面

<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="../../Angular/angular.min.js"></script>
</head>
<body>
<div sidebox title="Links">
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</div>
<div ng-controller="myController">
<div sidebox title="ttt">
<a href="" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{data}}</a>
<a href="" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >ddddd</a>
<a href="" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >dddd</a>
<a href="" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >dddd</a>
<a href="" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >ddddd</a>
</div>
</div><script>
angular.module("myApp",[])
.controller("myController",function($scope){
$scope.data = "大厨的笔记"
})
.directive("sidebox",function(){
return{
restrict:"EA",
scope:{
title:'@'
},
transclude:true,
template:'<div class="sidebox">' +
' <div>' +
' <h2>{{title}}</h2>' +
' <span ng-transclude>' +//把自定义指令里的代码放到带有ng-transclude指令的地方
' </span>' +
' </div>' +
'</div>'
}
})
</script>
</body>
</html>

controller

值是一个字符串表示在指定自定义指令的控制器字符串就是对应的控制器的名字,值还可以是一个函数,表示当前指令的一个匿名控制器。

<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="../../Angular/angular.min.js"></script>
</head>
<body>
<div my-directive title="123333"></div><div chef-controller></div>
<script>
angular.module("myApp",[])
//控制器
.controller("myController",function($scope){
$scope.data = "自定义指令的指定控制器"
}) //自定义指令指定控制器的名字
.directive("myDirective",function(){
return{
restrict:'A',
scope:{}, //指定自定义指令的控制器名字
controller:'myController',
template:'<a>{{title}}</a><span>{{data}}</span>'
}
}) //指定自定义指令的匿名控制器
.directive("chefController",function(){
return{
restrict:"A",
scope:{
title:'@'
},
template:'<a>{{title}}</a>', //指令内部的匿名控制器
controller:function($scope){
$scope.title = "内部定义匿名控制器";
}
}
})
</script>
</body>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290