Angualr - directive's controller VS Link

简短的答案:放在 LINK 里面。 为毛?问问自己一个问题,我的代码应该在什么时候执行? 1. 在模板编译之前执行?- controller 2. 在模版编译之后执行?- link controller 里面的代码会在编译之前执行,而 link 里面的代码会在编译之后执行。var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.name = 'First '; }); app.directive('exampleDirective', function ... read more

Angular 性能优化

尽量减少 WATCHER 的数量: 以下代码均会产生 watcher: * $scope.$watch * {{}} 数据绑定 * 大多数 ng directive,如 ng-show ,ng-hide * $scope 变量的属性及方法,如 $scope.method = function () {} * DOM filter,如 {{ title | greet}} * ng-repeat wacher 将会存储在各自相应的 $scope.$$wachers 里面。当有新的 wacher 产生时,则会自动添加到 $scope.$$acher 里面。 watcher 在以下情况下 ... read more

CSS escape

如果使用 querySelector 的话,传入的 selector 如果为数字的 class 或者 id 时会报错。但是使用 getElementById 以及 getElementByClass 则不会。这个是因为跟 CSS 的转义有关。> It is valid, but requires some special handling. From here: http://mathiasbynens.be/notes/css-escapes [http://mathiasbynens.be/notes/css-escapes] > Leading digits > If the first ... read more

Angular Tips

4种类型的 SCOPE: * 普通的子 scope ,继承父 scope 的方法和属性。 * ng-include * ng-switch * ng-controller * ng-if * directive 里的 scope: true 。 * 继承父 scope 的方法和属性,并且将父 scope 的属性顺便复制到新的子 scope 里面。 * ng-repeat 。 * 独立的 scope ,不从父 scope 上继承方法和属性。 * directive 里面的 scope: { .. ... read more

Preflighted requests

Preflighted requestsUnlike simple requests (discussed above), “preflighted” requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send. Cross-site requests are preflighted like this since they may have impl ... read more

JavaScript Trick (1)

1. 立即执行匿名函数 !function () { // code goes here }(); +function () { // code goes here }(); ~function () { // code goes here }(); 分析 以上代码作用都是声明一个匿名函数,并且立即执行。最常见的立即执行匿名函数的写法是:(function () { // code goes here })(); 先来解释一下这个常见的立即执行匿名函数的原理。整个代码块其实可以这样分析:( function () { } )() 其实就是看成是两对括号,然 ... read more

(function(window, undefined){})(window)这种写法的原理

在看一些 jQuery 插件的时候经常会看到这样的写法:(function(window, undefined){ ... //code goes here })(window) 或者这样的:(function($, window, undefined){ ... //code goes here })(jQuery, window) 其实上面两个例子从根本上讲是一样的。为何要在匿名函数中传入$和window和undefined形参呢?先来说说前两个形参。JavaScript 的解析器解析代码其实是从里到外解析的,也就是说,解析器会先解析函数内部的代码,然后再解析函数外部的代码。这样的话, ... read more

JavaScript 中的作用域和声明提升

作用域 JavaScript 中没有块级作用域,只存在函数作用域,函数是它的第一类。具体表现如下:for(var i = 0; i < 10; i++){ //do something } alert(i);//10 var example = function(){ var x = 10; //do something } alert(x);//ReferenceError: x is not defined 上面的例子应该可以清楚地解释了 JavaScript 的作用域问题。在for里面定义的变量其实都是全局变量。因为 JavaScript 没有块级作用域。然后,在 ... read more

<script> 标签应该放在 HTML 页面中的哪里?

首先说说浏览器在下载 JS 文件时情况是怎样的。在低版本浏览器中,当页面载入时,浏览器解析到 JavaScript 标签时,如果是外链 JS 文件的话,则会开始下载 JS 文件,并且会阻塞其他文件的下载,比如排在这个 JS 文件后面的 JS 文件或者图片等。在 IE 8, Firefox 3.5, Safari 4, Chrome 2 等高版本浏览器中是允许并行下载 JS 文件的。但是还是会阻塞其他文件如图片的下载。另外,当浏览器解析到<script></script>时都会暂停页面相应等待 JS 解析完毕和执行完毕。为什么呢?因为 JS 文件可能会对 DOM 进行操作,所以要获得完整的页面就 ... read more

在 JavaScript 中创建Object

> JavaScript 是一门强类型的面向对象语言,实质是 Prototype-based Language ,语言本身没有 类 的概念。在 JavaScript 一切都是以对象的形式存在,包括 Function , Array , Object , String 等。接下来谈谈如何创建 Object 。 创建对象 1. 工厂模式 工厂模式是以一个函数封装一系列属性和方法的形式创建对象。下面是例子:function createPerson(name,age,job){ var o = new Object(); o.name = name; o.age = age; ... read more