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

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

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

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

  1. $compile(html)($scope);

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

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

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

  1. $routeChangeStart -> $locationChangeStart -> $locationChangeSuccess -> $routeChangeSuccess

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

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

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

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

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

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

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

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

  1. var isAngularModuleExists = function(moduleName){
  2. try{
  3. angular.module(moduleName)
  4. }catch{
  5. return false
  6. }
  7. return true;
  8. };

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. javascript设计模式与开发实践阅读笔记(6)——代理模式

    代理模式:是为一个对象提供一个代用品或占位符,以便控制对它的访问. 代理模式的关键是,当客户不方便直接访问一个对象或者不满足需要的时候,提供一个替身对象来控制对这个对象的访问,客户实际上访问的是替身对 ...

  2. android: 接收和发送短信

    8.2    接收和发送短信 收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这 项功能,而 Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.每个 A ...

  3. nload 实时网速查看

    nload eth0 -u K Device eth0 [192.168.0.33] (1/1):=================================================== ...

  4. EWM一个仓库号对应ERP多个PLANT的配置

    1. CIF多个DC到EWM系统,创建SCU并配置成仓 2. tx:/sapapo/loc3 对多个DC生成分配模型 3. IMG:分配仓库编号 去掉缺省的授权处理方 4. 维护SCU hierarc ...

  5. Docker实践(2)—虚拟网络

    1 docker(container)的虚拟网络 docker的虚拟网络结构: host创建一个虚拟bridge,每个container对应一个虚拟网络设备(TAP设备),与bridge一起构成一个虚 ...

  6. sublime Text Pastry使用

    来源:   https://github.com/duydao/Text-Pastry/wiki/Examples Using a text list Using the Clipboard Clip ...

  7. eclipse maven 创建总POM 工程

    首先进入到eclipse的workspace,我这里的workspace目录是D:\workspace 1.创建总的POM D:\workspace>mvn archetype:create - ...

  8. likwid工具尝鲜

    likwid: like I knew what I am doing. 是一系列工具的集合,用于针对多线程程序的指标分析,方便程序员进行性能调优工作.可以深入到控制cpu等硬件的频率. 主要包括以下 ...

  9. WPF调用图片路径,或资源图片

    一.加载本项目的图片WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源.其中较为常见的情况是用Uri加载图像.Uri表达式的一般形式为:协议+授权 ...

  10. WPF中System.Diagnostics.Process.Start的妙用

    我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 那么如何做呢? 答案 ...