angularjs

发布时间: 2015-12-12 20:45:34 作者: 大象笔记

使用 AngularJS 写前端的节奏

如何访问后端的 RESTful 接口

使用 ngResource 而不是使用相对原始的 http

如何更好的理解 AngularJS

通过实际交互流程来理解

module 与 model 不是一回事

如何面对 angular-seed 生成的 5 个 js 文件

angular-seed 默认在 app/js 目录下产生了 5 个 js 文件。

实际上可以写成一个文件

angular.module('myModule', []).

  value('a', 123).

  factory('a', function() { return 123; }).

  directive('directiveName', ...).

  filter('filterName', ...);

分成多个文件的原因

The reason for this breakup is that in your tests, it is often necessary to
ignore the initialization code, which tends to be difficult to test. By
putting it into a separate module it can be easily ignored in tests. The tests
can also be more focused by only loading the modules that are relevant to
tests.

通过对比这 5 个文件,可以发现

例如, 
function ControllerName($log) {} 
ControllerName.$inject = ["$log"] 就是用到了 log service.

如何注册一个返回常量的 service

可以认为这是一个全局访问的常量, 在 services.js 中定义

angular.module("myApp.services", []).
  value("name", "Zhongwei Sun");

辟邪剑谱中最容易被忽视的 Dependency Injection

在 filters.js 以及 directives.js 中可以看到这样的写法

directive("appVersion", ["version", function(version) {}]);

这是一个 Inline Annotation,因为匿名的回调函数不方便通过设置 $inject 属性的方 法来解决依赖,所以采用这样的语法。

再例如,如果要使用 $log

directive("appVersion", ["$log", function($log) {}]);

实际上也可以写成

directive("appVersion", function($log) {});

但是还是推荐写成第一种形式。第一种形式可以称为显式依赖注入,与之对应的就是隐式 依赖注入。隐式依赖注入可以引起的问题:

假如我们需要压缩、混淆我们的代码,这可能会导致参数名称被更改,遇到这种情况的时 候,我们还是需要使用显式声明依赖的方式。

还有一种是 module 间的依赖,例如, 我要使用 ngResource module 中的 $resource:

<script src="lib/angular/angular-resource.js"></script>
ngular.module('phonecatServices', ['ngResource']).
    factory('Phone', function($resource){
  return $resource('phones/:phoneId.json', {}, {
    query: {method:'GET', params:{phoneId:'phones'}, isArray:true}
  });
});

参考:

AngularJS 这堆 js 文件的加载顺序是否有要求

AngularJS has dependency injection at its core, 小应用不需要考虑加载顺序。

directive 的命名规则

参考:

service path

return $resource("/todos/:todoId", {todoId: "@id"}

这里的 /todos 对应的即是根目录

Object # has no method 'push'

对于返回的 json 结构中包含错误码的情况,需要将 query 的 isArray 参数设置为 fasle. 否则会报上面的错误。然后在 controller 中对结果进行解析。

service action 什么时候需要加 $ 前缀

save, remove, delete 需要加 $ 前缀,但是只是在实例化的情况下。若是直接使用类的 delete 则不需要加此前缀。

使用 Masonry + AngularJS 实现瀑布流

代码示例:

AngularUI 与 Bootstrap UI 的区别

使用 AngularJS 的输入框验证

<form class="form" novalidate>
  <input type="text" required />
</form>

首先需要禁用掉浏览器自带的 form 验证,即使用 novalidate. 原因是,不同浏览器的 行为有差异。比如,chrome 对标记了 required 的输入框,在输入为空的情况下,自动 进行了提示。但是,其他浏览器就没有该行为,比如 safari. 所以需要使用统一的验证 方式。

controller getting invoked twice problem

问题,controller 里的逻辑执行了两次。

原因,controller 在 routing 以及 partial html 里定义了两次。

解决方法,去掉 partial 里的 ng-control 定义.

参考:

AngularJS 需要注意的问题

当N个 HTTP 请求需要顺序处理时,如何避免回调写法

对 ng-view 的 PV 统计

参考:

ng-cloak

The ngCloak directive is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading. Use this directive to avoid the undesirable flicker effect caused by the html template display.

ng-bind

It is preferrable to use ngBind instead of {{ expression }} when a template is momentarily displayed by the browser in its raw state before Angular compiles it. Since ngBind is an element attribute, it makes the bindings invisible to the user while the page is loading.

参考

Quick Links

我是一名山东烟台的开发者,联系作者