1、在使用$routeProvider的时候,需要让模块依赖ngRoute,否则会提示找不到服务,示例:

angular.module('module1', ['ngRoute'])
.config(['$routeProvider', function($routeProvider){
//do something...
}]);

2、在页面中需要绑定有风险的html的时候,可以使用ng-bind-html="html"(version>=1.3),如果遇到错误,控制器中可以使用html = $sce.trustHtml(unsafeHtml)

3、 如何动态的向页面添加带指令的HTML?通入如下代码:

$compile(html)($scope);

4、如果阻止事件冒泡?示例如下:

//方式一,利用一个自定义指令实现
.directive('stopEventPropagation', function(){
return {
restrict: 'A',
link: function(scope, iElement, iAttrs){
//通过获取事件对象,来阻止调用
iElement.bind('click', function(e){
e.stopPropagation();
});
}
}
}); <a stop-event-propagation ng-click="doSomething();">Click me</a> //方式二,直接引用$event对象 <a ng-click="doSomething(); $event.stopPropagation();">Click me</a>

5、关于$route和$location的事件顺序,如下:

$routeChangeStart -> $locationChangeStart -> $locationChangeSuccess -> $routeChangeSuccess

6、有关select标签的使用,当options的来源是ajax时,那么如果指定选中项呢?如下:

<select ng-options="sysOptions" ng-model="selectSystem"></select>
//如上HTML代码,如果sysOptions来自ajax请求,而selectSystem又不是的话,往往会选中一个空值。
//可以使用如下方式避免: .controller('TestCtrl', ['$scope', '$http', function($scope, $http){
$http.get(...).success(function(data){
$scope.sysOptions = data;
//在异步回调函数中,对ng-model赋值。
$scope.selectSystem = 'Test';
});
}]);

7、在编写指令时,属性的匹配大小写需要注意:如果在html中使用showName="xx",那么在指令的iAttrs中,应该使用showname获取。如果要在指令中使用showName获取的话,那么必须在html中使用show-name="xx"。

8、要想让ng-href="{{true: 'javascript:void(0);' : 'url'}}" 生成 href="javascript:void(0);"时,需要修改配置,代码如下:

.config(['$compileProvider', function($compileProvider){
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|javascript):/)
}]);

9、在ng-click等ng事件中,如果拿到事件源对象?如下:

<a ng-click="click($event);" />

$scope.click = function($event){
var target = $event.target;
}; //注意,如果使用ng-click="click($event.target)",将会导致angular解析错误。

10、判断angular的模块是否存在,可以使用如下代码:

var isAngularModuleExists = function(moduleName){
try{
angular.module(moduleName)
}catch{
return false
}
return true;
};

11、在使用coffee编写使用provider方式编写服务时,当心写在最后的this.$get,coffee会将最后一句编译为return this.$get,而这刚好不符合provider的要求,所以应该在末尾手动加上return或者放置一个undefined在最后,放置编译出return this.$get这样的代码。

12、如果要动态控制是否启用非空验证,可以使用ng-required="true|false"指令。

13、当心ng-if指令,在使用ng-if指令时,会创建独立的作用域,如果要在$scope监视ng-if包含的变量,那么是无法成功的。如果一定要监视,可以考虑使用ng-show。

14、注意.value()与.constant的区别,前者只能注入和用于服务或者控制器中,后则可以被注入到配置(.config(['xx']))中。

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

Angular开发Tips的更多相关文章

  1. Visual Studio Code作为Angular开发工具常用插件安装、json-server安装与使用、angular/cli安装失败问题

    前提准备: 搭建好Angular开发环境 1 安装Visual Studio Code 教程简单,不会的去问度娘 2 安装Chrome浏览器 教程简单,不会的趣闻度娘 3 Visual Studio ...

  2. Angular开发实践(一):环境准备及框架搭建

    引言 在工作中引入Angular框架将近一年了,在这一年中不断的踩坑和填坑,当然也学习和积累了很多的知识,包括MVVM框架.前后端分离.前端工程化.SPA优化等等.因此想通过Angular开发实践这系 ...

  3. Angular开发技巧

    由于之前有幸去参加了ngChina2018开发者大会,听了will保哥分享了Angular开发技巧,自己接触Angular也有差不多快一年的时间了,所以打算对Angular开发中的一些技巧做一个整理 ...

  4. angular开发控制器之间的通信

    一.指令与控制器之间通信,无非是以下几种方法: 基于scope继承的方式 基于event传播的方式 service的方式(单例模式) 二.基于scope继承的方式: 最简单的让控制器之间进行通信的方法 ...

  5. Spring MVC 学习笔记1 - First Helloworld by Eclipse【& - java web 开发Tips集锦】

    Spring MVC 学习笔记1 - First Helloworld by Eclipse reference:http://www.gontu.org 1. 下载 Spring freamwork ...

  6. angular开发中的两大问题

    一.在我们的angular开发中,会请求数据但轮播图等...在请求过数据后他的事件和方法将不再执行: 看我们的解决方案一: app.controller("text",functi ...

  7. Angular开发实践(七): 跨平台操作DOM及渲染器Renderer2

    在<Angular开发实践(六):服务端渲染>这篇文章的最后,我们也提到了在服务端渲染中需要牢记的几件事件,其中就包括不要使用window. document. navigator等浏览器 ...

  8. angular开发中对请求数据层的封装

    代码地址如下:http://www.demodashi.com/demo/11481.html 一.本章节仅仅是对angular4项目开发中数据请求封装到model中 仅仅是在项目angular4项目 ...

  9. Angular14 Visual Studio Code作为Angular开发工具常用插件安装、json-server安装与使用、angular/cli安装失败问题、emmet安装

    前提准备: 搭建好Angular开发环境 1 安装Visual Studio Code 教程简单,不会的去问度娘 2 安装Chrome浏览器 教程简单,不会的趣闻度娘 3 Visual Studio ...

随机推荐

  1. JNI开发示例

    安装:eclipse(http://www.eclipse.org/).CDT(C/C++ Development Tooling).ADT(Android Development Tools) ht ...

  2. C# 传入引用类型的参数 返回值是否发生变化

    前一段时间做项目是,一YY说如果一个方法的参数是引用类型,那么在这个方法里面所做的所有的修改再方法调用后应该有体现.事实是这样的吗? 先看code 和运行结果: 运行结果 方法SetPersonInf ...

  3. JavaScript 中数组实用浅析

    本文适用于HTML.ASP 中的 JavaScript 脚本代码.代码以 HTML 中的 JS 为例,如果在 ASP 中,请将 document.write 改为 Response.Write 即可. ...

  4. 【转】Xcode概览(Xcode 6版):循序渐进认识Xcode

    该系列文章翻译自苹果的Xcode Overview文档,对大部分开发者来说,已经非常熟悉Xcode的功能和特性,不过伴随着iOS 8 SDK的发布,Xcode 6中也有些许调整,所以对该文档进行了翻译 ...

  5. 文档大师 搜狗拼音无法输入汉字_乱码的解决方法_VB6程序

    文档大师用 搜狗拼音无法输入汉字,显示的内容和输入的内容不一致.解决方法: 把中文输入里面的那个“美式键盘”再删掉就好用了,只保留搜狗输入法即可!

  6. MongoDB副本集配置系列九:MongoDB 常见问题

    What is a namespace in MongoDB? If you remove a document, does MongoDB remove it from disk? When doe ...

  7. [Aaronyang] 写给自己的WPF4.5 笔记21 [3d课 2/4]

    1. 当然复杂的3d模型我们是可以通过更专业的工具做出来,然后导入项目中,我们只是方便演示,选择简单的图形. Tip: 关于摄像机的NearPlaneDistance和FarPlaneDistance ...

  8. jackson json转实体 允许特殊字符和转义字符 单引号

    //允许出现特殊字符和转义符 mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ; //允许出现单引号 mapper.confi ...

  9. jQuery中position()与offset()区别

    使用jQuery获取元素位置时,我们会使用position()或offset()方法,两个方法都返回一个包含两个属性的对象-左边距和上边距,它们两个的不同点在于位置的相对点不同. 可以看看下边的图: ...

  10. Test Tex

    \begin{equation}\label{exampleone}r = r_F+ \beta (r_M - r_F) + \epsilon\end{equation}