AngularJs中的directives(指令)

一、指令的职责

  指令的职责是修改DOM结构,并将作用域和DOM连接起来.即指令既要操作DOM,将作用域内的数据绑定到DOM节点上,又要为DOM绑定事件调用作用域内的对应的方法。

二、创建自定义指令

调用自定义指令的4种方式:元素、属性、样式类、注释.

常用的是前两种,实例如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body ng-app="myApp" >
		<hello></hello><!--E元素-->
		<div hello></div><!--A属性-->
		<div class="hello"></div><!--C样式类-->
		<!--directive:hello--> 

	</body>
	<script type="text/javascript" src="js/angular.js" ></script>
    <script type="text/javascript" src="js/directive.js" ></script>
</html>

js代码:

var app = angular.module("myApp",[]);
app.directive("hello",function(){
	return {
		restrict: ‘AECM‘,
		template:‘<div>Hi everyone</div>‘,//模板
		//templateUrl:‘index.html‘,
		replace: true
	}
});

测试结果只有3条内容,没有显示采用注释的方法调用指令的结果.不过常用的还是使用属性和元素的方式来调用指令.

三、指令定义中用到的其他字段

1.templateUrl

模板的显示还可以使用templateUrl属性来调用html文件中的DOM元素.

如果其他指令也要使用同一个模板,可以利用AngularJs中的$templateCache属性将模板缓存起来然后再调用:

用到angular中的run方法,它只会执行一次.

js文件:

var app = angular.module("myApp",[]);
//app.directive("hello",function(){
//	return {
//		restrict: ‘AECM‘,
//		template:‘<div>Hi everyone</div>‘,//模板
//		//templateUrl:‘index.html‘,
//		replace: true
//	}
//});

app.run(function($templateCache){
	$templateCache.put("hello.html","<div>Hello,my name is yo</div>");
});

app.directive("hello",function($templateCache){
	return {
		restrict: ‘A‘,
		//template:‘<div>Hi everyone</div>‘,//模板
		template:$templateCache.get("hello.html"),//模板
		//templateUrl:‘index.html‘,
		replace: true
	}
});

app.directive("hi",function($templateCache){
	return {
		restrict: ‘E‘,
		//template:‘<div>Hi everyone</div>‘,//模板
		template:$templateCache.get("hello.html"),//模板
		//templateUrl:‘index.html‘,
		replace: true
	}
});

测试结果:

2.transclude:是否为指令模板或编译函数提供指令元素中的内容.

不加这个字段,在指令中添加模板会替换掉之前有的内容,transclude是很重要的一个字段,它可以让指令与指令之间相互嵌套.

js关键代码:

效果:

3. link:定义将指令与作用域连接起来的链接函数

js文件:

var app = angular.module("myApp",[]);
app.controller("myController",[‘$scope‘,function($scope){
	$scope.loadData = function() {
		console.log("数据加载中...");
	};
}
]);

app.directive("loader",function($templateCache){
	return {
		restrict: ‘AE‘,
		link: function(scope,element,attr){
			element.bind("mouseenter",function(){
				scope.loadData();
			});
		}
		//template:‘<div>Hi everyone</div>‘,//模板
		//transclude: true,
		//template:"<div>Hi everyone<div ng-transclude></div></div>",
		//templateUrl:‘index.html‘,

	}
});

效果:

注:将scope.loadData();替换成scope.$apply("loadData()");可以达到同样的效果.

如果有多个controller指令的情况,如何调用方法?

采用用属性的方式来调用属性:

var app = angular.module("myApp",[]);
app.controller("myController",[‘$scope‘,function($scope){
	$scope.loadData = function() {
		console.log("数据加载中...");
	};
}
]);

app.controller("myController2",[‘$scope‘,function($scope){
	$scope.loadData2 = function() {
		console.log("数据加载中aaaa...");
	};
}
]);

app.directive("loader",function($templateCache){
	return {
		restrict: ‘AE‘,
		link:function(scope,element,attrs){
			element.bind("mouseenter",function(){
				//scope.loadData();
				scope.$apply(attrs.howtoload);
			});
		}
		//template:‘<div>Hi everyone</div>‘,//模板
		//transclude: true,
		//template:"<div>Hi everyone<div ng-transclude></div></div>",
		//templateUrl:‘index.html‘,

	}
});
时间: 2024-08-03 23:39:51

AngularJs中的directives(指令)的相关文章

angularJS中自定义验证指令中的$parsers and $formatters

本文翻译自:$parsers and $formatters in Custom Validation Directives in Angular JS 在使用angularJS的应用中,有时候我们需要定义自己的表单验证.自定义验证在angularJS中是通过创建指令来实现的,该指令依赖于ng-model指令,主要是依赖于它的controller. ng-model指令提供2个由函数组成的数组: $parsers 和 $formatters,这些函数用于实现自定义验证逻辑时调用.这两个数组的用途

angularjs中的分页指令

自定义指令 import angular from 'angular'; /** * @ngdoc module * @name components.page * @description 分页directive */ export default angular.module('pageDirective', []).directive('ngPage', function () { return { restrict: 'E', scope: { totalCount: '=', page

深入理解AngularJS中的ng-bind-html指令和$sce服务

angular js的强大之处之一就是他的数据双向绑定这一牛B功能,我们会常常用到的两个东西就是ng-bind和针对form的ng-model.但在我们的项目当中会遇到这样的情况,后台返回的数据中带有各种各样的html标签.如: $scope.currentWork.description = "hello,<br><b>今天我们去哪里?</b>" 我们用ng-bind-html这样的指令来绑定,结果却不是我们想要的.是这样的 hello, 今天我们

AngularJS 中{{}}与ng-bind指令

面试中,有被问题关于{{}}与ng-bind指令的问题,在此,分享下自己的知识点. 在脚本没有加载完成时,用户会看到{{}},界面比较丑陋. 一般的解决方法: 在index.html里面使用ng-bind,其它动态加载进来的内容使用{{}} ng-bind主要就是用户数据的展示.

angularJS中的ng-repeat指令!

ng-repeat 指令: ng-repeat 指令用来遍历一个数组重复创建当前元素: <ul ng-app="myApp" ng-controller="myAppController"> <li ng-repeat="item in userNames track by $index">{{$index}}:{{item}}</li> </ul> <script type="te

AngularJS中的指令

欢迎大家讨论与指导 : ) 明天天会继续更新本文O(∩_∩)O  前言 当AngularJS中的内置指令不能满足我们的需求,或者当我们需要创建一个能够用于多个AngularJS程序的自包含的功能单元时,我们应该创建自定义指令来满足需求.  一.创建自定义指令   一 . 1 命名规则  我们要在创建指令时使用峰驼式命名,例如指令是 <div unordered-list></div>  在声明指令时我们需要这样子写 app.directive("unorderedList

angularJS中的ng-show、ng-if指令

angularJS中的ng-show.ng-hide.ng-if指令都可以用来控制dom元素的显示或隐藏. 1. ng-show和ng-hide 根据所给表达式的值来显示或隐藏HTML元素.元素会渲染出来,只是通过css隐藏了. 即DOM中会存在, 通过选择符可以获取到此元素的. 2.ng-if指令 可以根据表达式的值在DOM中生成或移除一个元素.如果赋值给ng-if的表达式的值是false,那对应的元素将会从DOM中移除(DOM中不会存在, 通过选择符无法选择到),否则生成一个新的元素插入DO

angularJS中自定义指令

学习了angularJS一周,但是大部分时间被自定义指令占用了.博主表示自学互联网好心塞的,发现问题的视觉很狭窄,这比解决问题要更难.这篇文章首先介绍了自定义,然后介绍了在使用自定义指令遇到的问题. 代码模板: var myModule = angular.module("myModule",[]); myModule.directive('directiveName',function(){ return{ restrict:string, template:string, temp

浅谈AngularJS中的指令和指令间的相互通信

说到AngularJS,我们首先想到的大概也就是双向数据绑定和指令系统了,这两者也是AngularJS中最为吸引人的地方.双向数据绑定呢,感觉没什么好说的,那么今天我们就来简单的讨论下AngularJS这个框架的指令系统. 指令作为AngularJS中最为重要的部分,所以这个框架本身也是自带了比较多的的指令,但是在开发中,这些指令通常不能满足我们的需要,所以我们也是需要自定义一些指令的.那么一个AngularJS指令在HTML代码中可以有四种表现形式: 1.作为一个新的HTML元素来使用. <h