index01.html
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript" src="../js/angular-1.3.14.min.js" >script>
<script type="text/javascript" src="index01.js" >script>
head>
<body ng-controller="myController">
<div my-directive>div>
<my-directive>my-directive>
<div class="myDirective">div>
body>
html>
index01.js
angular.module("myApp",[],['$compileProvider',function($compileProvider){
$compileProvider.directive("myDirective",function(){
return {
"restrict":"ECMA",/*E-element,C-class,M 注释,A-attribute*/
"template":"hello,directive
",
'replace':true/*将html页面引用这个指令的标签替换掉*/
}
});
}])
.controller("myController",function($scope){});
index02.html
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript" src="../js/angular-1.3.14.min.js" >script>
<script type="text/javascript" src="index02.js" >script>
head>
<body>
<div my-directive>div>
<my-directive>my-directive>
<div class="myDirective">div>
body>
html>
index02.js
angular.module("myApp",[])
.directive("myDirective",function(){
return {
"restrict":'ECMA',
"template":"hello,directive1.
",
"replace":true//用replace时,必须要有一个节点包裹才能替换
/*
* templateUrl:导入模板的路径
*
* */
}
});
index03.html
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript" src="../js/angular-1.3.14.min.js" >script>
<script type="text/javascript" src="index03.js" >script>
head>
<body>
<div my-directive>div>
<my-directive>my-directive>
<div class="myDirective">div>
body>
html>
index003.html
this is a templateUrl test.
index03.js
angular.module("myApp",[])
.directive("myDirective",function(){
return {
"restrict":"ECMA",
"templateUrl":"index003.html",
"replace":true
}
});
index.html
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript" src="../js/angular-1.3.14.min.js" >script>
<script type="text/javascript" src="index04.js" >script>
head>
<body>
<div my-directive1>this is my div.div>
body>
html>
i
ndex.js
angular.module("myApp",[])
.directive("myDirective1",function(){
return {
"restrict":"ECMA",
"template":"this is directive1.
",
"replace":true,
"transclude":true//保存原标签内的值
}
});
index.html
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript" src="../js/angular-1.3.14.min.js" >script>
<script type="text/javascript" src="index05.js" >script>
head>
<body>
<div my-directive1 my-directive2>div>
body>
html>
index.js
angular.module("myApp",[])
.directive("myDirective1",function(){
return {
"restrict":"ECMA",
"template":"this is directive1.
",
"replace":true,
"priority":0
}
})
.directive("myDirective2",function(){
return {
"restrict":"ECMA",
"template":"this is directive2.
",
"replace":true,
"priority":10,
"terminal":true
}
});
对特定的元素注册事件,需要用到scope参数来实现dom元素的一些行为
index.html
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript" src="../js/angular-1.3.14.min.js" >script>
<script type="text/javascript" src="index06.js" >script>
head>
<body>
<div my-directive>div>
body>
html>
index.js
angular.module("myApp",[])
.directive("myDirective",function(){
return {
"restrict":"ECMA",
"template":"this is testController.
",
"controller":function($scope){
console.log("controller...");//进行值的传递
},
"compile":function(tElement,tAttris,transclude){
/*
* compile:返回值就是link函数,在指令编译过程的第三阶段运行
*
* */
return {
//编译阶段之后,指令连接子元素之前
"pre":function preList(){
console.log("pre...");
},
//在编译阶段之后,指令连接子元素之后
"post":function postList(){
console.log("post");
}
}
},
}
});
index.html
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript" src="../js/angular-1.3.14.min.js" >script>
<script type="text/javascript" src="index08.js" >script>
head>
<body>
<div my-directive>div>
body>
html>
index.js
angular.module("myApp",[])
.directive("myDirective",function(){
return {
"restrict":"ECMA",
"template":"this is controller test.
{{name}}",
"controller":function($scope){
$scope.name='this is controller.';
},
"controllerAs":"myControl",
"link":function(scope,iElement,iAttris,myControl){
console.log(scope.name);
iElement.on("click",function(){
alert("hello,angularjs.")
});
}
}
});
index.html
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript" src="../js/angular-1.3.14.min.js" >script>
<script type="text/javascript" src="index07.js" >script>
head>
<body>
<div my-directive>div>
body>
html>
index.js
angular.module("myApp",[])
.directive("myDirective",function(){
return {
"restrict":"ECMA",
"template":"- {{book.name}}
",
"controller":function($scope){
$scope.books=[{
"name":"嘻游记",
"id":12
},{
"name":"东游记",
"id":13
}];
this.addBook=function(){
$scope.$apply(function(){
$scope.books.push({
"name":"哈游记",
"id":11
});
});
}
},
"controllerAs":"booklist",
"replace":true
}
})
.directive("bookAdd",function(){
return {
"restrict":"ECMA",
"require":"^myDirective",
"template":"",
"replace":true,
"link":function(scope,iElement,iAttris,booklist){
iElement.on("click",booklist.addBook);
}
}
});
var myApp = angular.module("myApp",[]);
myApp
.directive("myDirective",function() {
return {
"restrict" : "AEMC",
"template" : "- {{book.name}}
",
"controller" : function($scope) {
console.info($scope)
},
/**
* scope默认为false,表示使用同一个作用域,
* 但是当我们设置为true时,此时,就是controller
* 的作用域就是一个独立的作用域
*/
"scope" : true,
"replace" : true
}
})
.controller("myCon",["$scope",function($scope) {
console.info($scope)
}]);
index.html
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript" src="../js/angular-1.3.14.min.js" >script>
<script type="text/javascript" src="index09.js" >script>
head>
<body ng-controller="myCon">
{{books}}==={{msg}}
<div my-directive bb="books" parent-books="books" scope-books="{{msg}}">div>
body>
html>
index.js
var myApp = angular.module("myApp",[]);
myApp
.directive("myDirective",function() {
return {
"restrict" : "AEMC",
"template" : "- {{book.name}}====={{msg}}
",
"controller" : function($scope) {
// console.info($scope.$parent.books)
// $scope.books = $scope.$parent.books;
$scope.books = $scope.aaa();
// $scope.books = $scope.bbb;
// $scope.bbb.push({
// "name" : "PHP",
// "price" : 30
// });
console.info($scope.ccc)
$scope.msg = $scope.ccc;
$scope.msg = "哈哈";
},
/**
* scope默认为false,表示使用同一个作用域,
* 但是当我们设置为true时,此时,就是controller
* 的作用域就是一个独立的作用域
*/
// "scope" : true,
/**
* 当scope是一个对象的时候,作用域也是一个
* 独立的作用域,但是无法共享父级作用域
*/
"scope" : {
//将父元素中的books封装成a函数
aaa: "&bb",
//数据与父元素双向绑定
// bbb: "=parentBooks",
//注意,只能传递字符串等基本数据类型,
//子类修改值,父类不会受到影响
ccc: "@scopeBooks"
},
"replace" : true
}
})
.controller("myCon",["$scope","$rootScope",function($scope,rs) {
$scope.books = [
{
"name" : "javascript",
"price" : 15.6
},{
"name" : "java",
"price" : 20
}
];
$scope.msg = "this is a test.";
// console.info($scope);
// console.log(rs)
}]);