一些常见的错误

  1. 在angularjs里,创建directive时,directive的名称应该要使用驼峰式,例如myDirective,而在html里要调用它的时候,就不能用驼峰式了,可以用my-directive或者my_directive,一般都使用前者吧,统一下规范。
  2. 若声明了compile函数,link函数就不会被调用

transclude

transclude有三个选项,true, false, 和object.如果不显示指明的话,默认为false.

当为false的时候,则那个directive里面的指令不会嵌入到你写的模板里,举个例子

下面是html代码

<div my-directive>
<h1>Hello</h1>
<div>all the content will be erased</div>
</div>

下面是javascript代码

angular.module("app")
.directive("myDirective", function() {
return {
restrict: "A",
template: "<div>This is my template</div>"
}
})

运行结果如下图:

这时候会发现,原来你在div里面写的内容全部不见了,直接被template覆盖了,这就是使用transclude:false的情况,原来那个directive里面的内容都不会嵌入到你写的模板来。

当然,我们可以使用transclude:true来解决这个问题。

只改变原来的directive代码,如下:

.directive("myDirective", function() {
return {
restrict: "A",
transclude: true,
template: "<div>This is my template</div> <div ng-transclude></div>"
}
})

运行结果如下图:

这个时候,原来那个div里面的所有内容都被放置到了ng-transclude声明的那个div里面了。因此只需要把transclude设置为true,然后在你的template里,在你想要原来指令放置在那里的地方,加一个ng-transclude,就可以将其放在里面.

那么,第三个选项object到底是干嘛的呢?还是让我们用一个例子来说明

将之前的html代码修改如下:

    <div my-directive>
<transclude-name>CJG</transclude-name>
<transclude-school>SYSU</transclude-school>
<h1>路人甲</h1>
<h2>路人乙</h2>
</div>

以及javascript代码如下:

    .directive("myDirective", function() {
return {
restrict: "A",
transclude: {
"name": "?transcludeName",
"school": "?transcludeSchool"
},
template: "<div>This is my template</div> <div ng-transclude></div>" +
"<div ng-transclude='name'></div> <div ng-transclude='school'></div>";
}
})

在这里,我们在html里增加了一些标签,然后在transclude里,给一些标签设置了一些名字,然后我们就可以在template里,让ng-transclude="你设置的名字"来将你某些特定的内容放在特定的位置,当然,你如果直接使用ng-transclude的话,就默认将所有你没有设置名字的标签全部移到里面.(这里在标签名字前面增加一个?号,是为了防止标签不存在而报错)

运行结果如下:



到这里,transclude的几个属性值就已经介绍完了,然而transclude还有一个坑,就是你如果不做特殊处理的话,他会创建一个单独的作用域,与外界分隔开,这就会导致你无法访问到之前的变量,还是让我们来看一个例子.

这里,我们先写了一个controller,里面只有一个$scope.name变量

(function() {
var app = angular.module("app", []);
angular
.module("app")
.controller("myCtrl", function($scope) {
$scope.name = 'CJG';
})
})()

之后,我们在html里,将$scope.name显示出来

<div ng-controller="myCtrl" my-exam>
<h1>my</h1>
<h1>{{name}}</h1>
</div>

然后,我们像之前一样,写一个简单的directive,利用ng-transclude将原来div的内容放到我们指定的区域内

angular.module("app")
.directive("myExam", function($compile) {
return {
restrict: "A",
transclude: true,
template: "<h1>Hello</h1> <div ng-transclude></div>"
}
})

那么,运行结果会不会和我们预期的一样,在ng-transclude里显示两个h1标签呢?让我们来看下



由上图可知,只显示了一个h1,而那个{{name}}没有显示出来,那么他有渲染吗?



由上图可以看到,他是有渲染两个div的,可是为什么就是没有值呢?原因就是因为,你使用transclude的话,默认是会创建一个新的作用域的,因此你就无法访问到之前作用域的值了。那么,怎么解决这个问题呢?

看了很多资料,我觉得比较有用的解决方法是以下两个:

1.使用transclude函数来将解决。transclude的函数原型为: transclude(scope, function(clone){}),我们可以将这个directive的scope传入给他,这样transclude就不会默认产生新的作用域,而是沿用我传给他的那个作用域,当然,你也可以根据自己的需求,传入你想传给他的scope,代码修改如下:

angular.module("app")
.directive("myExam", function($compile) {
return {
restrict: "A",
transclude: true,
template: "<h1>Hello</h1>",
compile: function(elem, attrs, transclude) {
return function(scope, element, attrs) {
transclude(scope, function(clone) {
elem.append(clone);
})
}
}
}
})

运行效果如下:



此时,就可以正常运转了。不过这个必须依赖于complie函数,然后通过他返回的link函数给transclude的内容一个作用域,然后将transclude的内容加载到页面里。

2.这种方法,其实讲道理根本不算用transclude的做法,不过也算是一种方法吧。。。。

angular.module("app")
.directive("myExam", function($compile) {
return {
restrict: "A",
link: function(scope, element, attrs) {
var subScope = scope.$new(true);
var newNode = $compile("<h1>Hello</h1>")(subScope);
element.append(newNode);
}
}
})

其实就是将你要加入的内容在link里面编译,然后用$scope.$new为它创建一个作用域,然后把它加到里面去。(我也不知道这算不算方法)

require

这个参数是用来加载其他directive的controller用的,比如你可能需要到父元素的controller的一些变量或者方法,那么你就可以通过他来访问父元素的controller ,示例如下:

angular.module("app")
.directive("myExam", function($compile) {
return {
restrict: "A",
require: "?^myParent",
link: function(scope, element, attrs, ctrl) {
console.log(ctrl);
ctrl.sayHello();
var subScope = scope.$new(true);
var newNode = $compile("<h1>Hello</h1>")(subScope);
element.append(newNode);
}
}
})
.directive("myParent", function() {
return {
restrict:"A",
controller: function myParent($scope) {
vm = this;
vm.name ="CJG"
vm.sayHello = function() {
console.log("hello");
}
}
}

html代码如下:

    <div my-parent>
<div ng-controller="myCtrl" my_exam>
<h1>my</h1>
<h1>{{name}}</h1>
</div>
</div>

在这里,我们在my-exam的directive里引用了他的父亲my-parent的控制器,然后调用了它的方法,运行效果如下:



加?^的前缀是为了防止找不到控制器的时候,报错,让整个程序崩掉,具体的前缀情况如下:

    • (no prefix) - Locate the required controller on the current element. Throw an error if not found.
    • ? - Attempt to locate the required controller or pass null to the link fn if not found.
    • ^ - Locate the required controller by searching the element and its parents. Throw an error if not found.
    • ^^ - Locate the required controller by searching the element's parents. Throw an error if not found.
    • ?^ - Attempt to locate the required controller by searching the element and its parents or pass
  • null to the link fn if not found.
    • ?^^ - Attempt to locate the required controller by searching the element's parents, or pass
  • null to the link fn if not found.

restrict

restrict有四个选项,"A"(属性值),"E"(元素),"C"(类名),"M"(评论)

比如你将一个声明为E的话,那么你只能通过来调用它,不过一个directive可以声明为多个选项.

template

一个html段

templateUrl

类似于html段,不过就是将它单独写在一个文件里,通过url异步加载进来,compile在它的加载过程中,即使之前使用缓存,它也会去执行别的directive的编译工作,这样就不会导致阻塞。

compile

该函数有三个参数,tElement,tAttrs,transclude,该函数主要用于改变template DOM,而link函数则主要用于注册一些监听事件和执行directive的大多数逻辑.

这个时候就涉及到html的一个渲染过程了:

  1. 浏览器先加载所有的html标识,将其转化为DOM,当浏览器遇到angularjs的时候,就会停止解析过程,去执行angularjs
  2. angularjs在DOM中搜索ng-app执行,若搜索到,则初始化一些必要的组件(即$injector、$compile服务以及$rootScope),然后从该元素开始执行angular的编译
  3. angularjs查看整一棵树,如果发现有directive,则将directive以及它的compile函数一起加入到待编译组里,等全部搜索完毕后,在根据他们的优先级对他们进行依赖注入和编译
  4. 编译运行完后,就会执行它们的链接函数,注册一些监听事件

具体的详情可以去看https://my.oschina.net/brant/blog/419641,我觉得这篇博客说的很清楚.

link

该函数有5个参数,scope, iElement, iAttrs, controller, transcludeFn

controller我们可以通过之前的require属性传过来。

attrs

directive可以利用attrs来做很多事情,比如,通过attr来访问共同的attribute对象,可以通过$observe来观察attribute值的变化

  .directive("myExam", function($compile) {
return {
replace: true,
restrict: "A",
require: "?^myParent",
link: function(scope, element, attrs, ctrl) {
// console.log(ctrl);
ctrl.sayHello();
scope.school = scope.$eval(attrs.ngModel);
var newNode = $compile("<h1>Hello {{ name }} </h1> <h1>school {{ school}}</h1>")(scope);
element.append(newNode);
scope.$watch(
function() {
return scope.$eval(attrs.ngModel);
},
function() {
scope.school = scope.$eval(attrs.ngModel)
})
}
}
})

angularjs directive学习心得的更多相关文章

  1. angularJS Directive学习

    Directive 指令 直接上实例 index.html <!doctype html> <html ng-app="drag"> <head> ...

  2. AngularJS Directive 学习笔记

    指令 Directive 指令要点 大漠老师的教学节点 解析最简单的指令 hello: 匹配模式 restrict 解析最简单的指令 hello: template.tempmlateUrl.$tem ...

  3. 学习AngularJs:Directive指令用法(完整版)

    这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下   本教程使用AngularJs版本:1.5.3 AngularJs GitHub: http ...

  4. 学习AngularJs:Directive指令用法

    跟我学AngularJs:Directive指令用法解读(上) http://blog.csdn.net/evankaka/article/details/51232895 跟我学AngularJs: ...

  5. Angularjs directive全面解读(1.4.5)

    说到Angularjs directive即指令,可以这么说Angularjs的灵魂就是指令,学会Angularjs指令那么你的Angularjs的武功就修炼了一半了,当然这只是鄙人的一点点独到见解, ...

  6. AngularJs:Directive指令用法

    摘自:http://www.jb51.net/article/83051.htm 摘要:Directive(指令)是AngularJ非常强大而有有用的功能之一.它就相当于为我们写了公共的自定义DOM元 ...

  7. 【js类库AngularJs】学习angularJs的指令(包括常见表单验证,隐藏等功能)

    [js类库AngularJs]学习angularJs的指令(包括常见表单验证,隐藏等功能) AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀 ...

  8. 50.AngularJs directive详解及示例代码

    转自:https://www.cnblogs.com/best/tag/Angular/ 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: https://github ...

  9. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

随机推荐

  1. 计算机网络——TCP三次、四次握手详解

    三次握手:建立TCP连接 连接建立过程: B的TCP服务器进程先创建传输控制块TCB(存储了每一个连接中的一些重要信息,如:TCP连接表,到发送和接收缓存的指针,到重传队列的指针,当前的发送和接收序号 ...

  2. ArcEngine 通过IRelationalOperator.Relation判断几何体相交

    IRelationalOperator 接口: 1. Provides access to members that determine if a certain spatial relationsh ...

  3. linux下ubuntu系统安装及开发环境配置

    1.安装系统:别的没什么说的,就是安的时候把网线拔了,不然到 configure apt的时候会卡起很久不走的2.配置网络 编辑/etc/network/interface打开/etc/networt ...

  4. source 命令

    作用: 当我修改了/etc/profile文件,我想让它立刻生效,而不用重新登录:这时就想到用source命令,如:source /etc/profile 介绍:source命令也称为“点命令”,也就 ...

  5. bootstrap按钮加载状态改变

    bootstrap里面有个激活按钮的时候,按钮变成不可用的: 按照官网里面的方法介绍是在button按钮加个data-loading-text="Loading..."属性,然后j ...

  6. How do I list all tables/indices contained in an SQLite database

    How do I list all tables/indices contained in an SQLite database If you are running the sqlite3 comm ...

  7. hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005

    废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...

  8. hdu 1527(威佐夫博奕)

    题意:容易理解. 分析:威佐夫博奕的模板题. 代码实现: #include<stdio.h> #include<string.h> #include<math.h> ...

  9. 多数据源问题--Spring+Ibatis 访问多个数据源(非分布式事务)

    有的时候,我在一个工程中需要访问两个以上的数据源,尤其是在系统集成的时候,以下是我在系统集成的时候遇到的情况,我的工程的架构是:spring2.0+ibatis2.0+struts1.2. 数据库是o ...

  10. MongoDB之一介绍(MongoDB与MySQL的区别、BSON与JSON的区别)

    MySQL与MongoDB的操作对比,以及区别 MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL ...