Angular从0到1:function(上)

时间:2022-09-19 22:53:06

1、前言

Angular作为最流行的前端MV*框架,在WEB开发中占据了重要的地位。接下来,我们就一步一步从官方api结合实践过程,来学习一下这个强大的框架吧。

Note:每个function描述标题之后的★标明了该function的重要程度(1~5星)。

2、function(上)

Angular封装了一系列公共方法,帮助我们更简单的使用JS。这些就是Angular的function。

2.1、angular.bind(★)

angular.bind类似于Function.prototype.bind,实现函数柯里化,返回一个函数代理。eg:

函数原型
angular.bind(/*this对象*/self, /*要封装的function*/fn, /*参数列表*/args); //原始函数
function fun(arg1, arg2, arg3) {
console.log(this);
console.log(arg1);
console.log(arg2);
console.log(arg3);
}
fun('arg1', 'arg2', 'arg3');
//如果几个常用参数都是固定的,而且该函数被调用频繁,那么就可以包装一下
var fun2 = angular.bind(window, fun, 'arg1', 'arg2');
//新的调用方式
fun2('arg3');

2.2、angular.bootstrap(★★★★)

用于使用angular执行渲染元素。也是angular的启动方法,如果没有在页面上指定ng-app,必须要手动调用该函数进行启动。

angular.bootstrap(/*Dom元素*/element, /*Array/String/Function*/[modules], /*Object*/[config]);

//常规用法
angular.bootstrap(document, ['app'])
//带配置项
angular.bootstrap(document, ['app'], {strictDi: true/*Defaults: false*/})

2.3、angular.copy(★★★★★)

Angular.copy用于复制对象,由于angular的双向绑定特点,那么如果直接操作$scope对象,那么很容易就会改变ui的显示,这个时候就需要借助angular.copy来创建一个对象副本,并进行操作。

//原型
angular.copy(source, [destination]); var obj = {a: 1};
var obj2 = angular.copy(obj);
var obj3;
angular.copy(obj, obj3);
console.log(obj2 === obj) //false
console.log(obj3 === obj) //false
var obj4;
//第二个和参数和返回值是相等的,而且第二个参数不管以前是什么,都会被重新赋值
var obj5 = angular.copy(obj, obj4);
console.log(obj4 === obj5); //true

2.4、angular.element(★★★★)

等价与jQuery的选择器,如果在angular之前没有引入jQuery,那么就使用jqLite包装.

angular.element('body');
//等价于
$('body'); //用法
var $body = angular.element('body');

2.5、angular.equals(★★)

用于比较两个对象是否相等,如下示例的规则和JS有区别,注意识别。

var obj1 = {a: 1};
var obj2 = obj1;
//引用一致,则相等
console.log(angular.equals(obj1, obj2)); // true obj2 = {a: 1};
//引用不一致,对象表现一致,则相等
console.log(angular.equals(obj1, obj2)); // true obj2 = {a: 1,$a: 2};
//对象比较时,忽略以$开头的属性
console.log(angular.equals(obj1, obj2)); // true obj1 = /aa/;
obj2 = /aa/;
//正则表达式表现相等,则相等
console.log(angular.equals(obj1, obj2)); // true //NaN与NaN比较,则相等
console.log(angular.equals(NaN, NaN)); // true

2.6、angular.extend(★★)

功能上和jQuery.extend没多大差异

//原型-第一个参数为目标,之后的参数为源,同时返回dst
angular.extend(dst, src); //示例
var obj1 = {a: 1};
var obj2 = angular.extend(obj1, {a: 2}, {b: 3});
console.log(obj1)
console.log(obj1 === obj2); //true

2.7、angular.forEach(★★★★★)

angular.forEach用于遍历对象或者数组,类似于ES5的Array.prototype.forEach

//原型
angular.forEach(obj, iterator, [context]); var values = {name: 'misko', gender: 'male'};
var arr = ['misko', 'male'];
angular.forEach(values, function (value, key) {
console.log(key + ' = ' + value);
});
angular.forEach(arr, function (value, i) {
console.log(i + ' = ' + value);
});
//还可以传递this
var obj = {};
angular.forEach(values, function (value, key) {
obj[key] = value;
}, obj);
console.log(obj);

2.8、angular.fromJson(★★★)

angular.fromJson将JSON字符串转换为JSON对象,注意,必须严格满足JSON格式,比如属性必须双引号,该函数内部实现是利用JSON.parse()。

//原型
angular.fromJson(/*string*/ jsonString) var jsonString = '{"p1": "xx", "p2": 1, "p3": true}';
var jsonObj = angular.fromJson(jsonString);
console.log(jsonObj);

2.9、angular.toJson(★★★)

angular.toJson可以将对象,数组,日期,字符串,数字转换为json字符串

//原型
angular.toJson(obj, pretty); var obj = {p1: 1, p2: true, p3: '2'};
var jsonString = angular.toJson(obj);
console.log(jsonString);
//美化输出格式(设置为true,默认采用2个字符缩进)
jsonString = angular.toJson(obj, true);
console.log(jsonString);
//还可以设置缩进字符数
jsonString = angular.toJson(obj, 10);
console.log(jsonString);

2.10、angular.identity(★)

angular.identity返回该函数参数的第一个值。编写函数式代码时,非常有用(待使用)。

//官方示例
function transformer(transformationFn, value) {
return (transformationFn || angular.identity)(value);
};
//简单演示
var value = angular.identity('a', 1, true);
console.log(value); // 'a'

2.11、angular.injector(★★)

angular.injector能够创建一个injector对象,可以用于检索services以及用于依赖注入。

//原型,[strictDi]默认false,如果true,表示执行严格依赖模式,
//angular则不会根据参数名称自动推断类型,必须使用['xx', function(xx){}]这种形式。
angular.injector(modules, [strictDi]); //定义模块A
var moduleA = angular.module('moduleA', [])
.factory('F1', [function () {
return {
print: function () {
console.log('I\'m F1 factory');
}
}
}]);
var $injector = angular.injector(['moduleA'])
$injector.invoke(function (F1) {
console.log(F1.print());
});
//此种写法会报错,因为是严格模式
var $injector2 = angular.injector(['moduleA'], true)
$injector2.invoke(function (F1) {
console.log(F1.print());
});
//可以采用如下写法
$injector2.invoke(['F1', function (F1) {
console.log(F1.print());
}]);

2.12、angular.module(★★★★★)

angular.module可以说是最常用的function了。通过它,可以实现模块的定义,模块的获取。

//定义模块A
var moduleA = angular.module('moduleA', []);
//定义模块B,模块B依赖moduleA
var moduleB = angular.module('moduleB', ['moduleB']); //可以在第三个参数上设置配置函数
var moduleB = angular.module('moduleB', ['moduleB'], ['$locationProvider', function ($locationProvider) {
console.log($locationProvider);
}]);
//等价于
var moduleB = angular.module('moduleB', ['moduleB'])
.config(['$locationProvider', function ($locationProvider) {
console.log($locationProvider);
}]); //获取模块
angular.bootstrap(document, ['moduleB']);

*: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从0到1:function(上)的更多相关文章

  1. [转贴]有关Angular 2.0的一切

    对Angular 2.0的策略有疑问吗?就在这里提吧.在接下来的这篇文章里,我会解释Angular 2.0的主要特性区域,以及每个变化背后的动机.每个部分之后,我将提供自己在设计过程中的意见和见解,包 ...

  2. Angular从0到1:function(下)

    1.前言 2.function(下) 2.13.angular.isArray(★★) angular.isArray用于判断对象是不是数组,等价于Array.isArray console.log( ...

  3. Angular 2.0 从0到1:Rx--隐藏在Angular 2.x中利剑

    第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...

  4. Angular 2.0 的设计方法和原则

    转载自:Angular 2.0 的设计方法和原则 在开始实现Angular 2.0版本之际,我们认为应该着手写一些东西,告诉大家我们在设计上的考虑,以及为什么做这样的改变.现在把这些和大家一起分享,从 ...

  5. rbenv安装ruby2.3.0在线安装不上。老子出绝招了(更新)

    今天把系统换成Linux mint了.感觉比ubuntu的好用太多,细节真是不错,Ubuntu感觉就是毛坯房,Linux mint真是精装修啊 问题来了.安装rbenv后,然后安装rbenv-buil ...

  6. MVC 5.0(or5.0↓) Ajax.BeginForm 异步上传附件问题,答案是不能的!

    MVC 5.0(or5.0↓)  Ajax.BeginForm 异步上传附件问题,答案是不能的! (请注意我这里说的异步!) 来看一下下面这段一步提交file的代码 //前台 .cshtml 文件 & ...

  7. Angular 2.0 从0到1 (七)

    第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...

  8. Angular 2.0 从0到1 (六)

    第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...

  9. Angular 2.0 从0到1 (四)

    第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...

随机推荐

  1. iOS 2D绘图 (Quartz2D)之阴影和渐变(shadow,Gradient)

    原博地址:http://blog.csdn.net/hello_hwc/article/details/49507881 Shadow Shadow(阴影) 的目的是为了使UI更有立体感,如图 sha ...

  2. org.springframework.orm.hibernate3.LocalSessionFactoryBean的疑惑解决办法

    在项目中使用了SSH框架(Struts2 + Spring3+ Hibernate3),applicationContext中配置了sessionFactory <bean id="s ...

  3. 【基础数学】质数,约数,分解质因数,GCD,LCM

    1.质数: 质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能整除以其他自然数(质数),换句话说就是该数除了1和它本身以外不再有其他的因数. 2.约数: 如 ...

  4. JLink 在J-Flash ARM批处理自动下载

    "C:\Program Files\SEGGER\JLinkARM_V420c\jflasharm.exe" -openprj.\stm32f100c8.jflash -open. ...

  5. Java基础知识强化之IO流笔记39:字符流缓冲流之复制文本文件案例01

    1. 字符流缓冲流之复制文本文件案例 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中 数据源: a.txt -- 读取数据 -- 字符转换流 -- InputStreamRe ...

  6. 跟我一起学extjs5&lpar;22--模块Form的自己定义的设计&rpar;

    跟我一起学extjs5(22--模块Form的自己定义的设计)         前面几节完毕了模块Grid的自己定义,模块Form自己定义的过程和Grid的过程类似,可是要更复杂一些.先来设计一下要完 ...

  7. Payload Inject And Fake

    常见捆绑注入payload手法 Payload捆绑注入 注入exe型+编码: msfvenom -a <arch> --plateform <platform> -p < ...

  8. Make3D Convert your image into 3d model

    Compiling and Running Make3D on your own computer source: http://make3d.cs.cornell.edu/code_linux.ht ...

  9. linux&lpar;ubuntu&rpar;GCC编译包含库函数的问题

    GCC 编译命令通常为:gcc hello.c -o hello.out 注意:若hello.c中引用有库函数(比如math.h),直接编译会出错 "/tmp/ccalvMPY.o: In ...

  10. Java LinkedList工作原理及实现

    1. 概述 以双向链表实现.链表无容量限制,但双向链表本身使用了更多空间,也需要额外的链表指针操作. 按下标访问元素—get(i)/set(i,e) 要悲剧的遍历链表将指针移动到位(如果i>数组 ...