1、angular.extend

  var dst = {name: 'xxx', country: 'China'};
  var src1 = {name: 'yyy', age: 10};
  var src2 = {sex:'famale',height:'tall'};
  angular.extend(dst,src1,src2);//You can specify multiple src objects.可以指定多个源对象
  console.log(dst);

  console.log(src1);

  

  从输出结果可以看出如果源元素中有与destination中相同的元素属性时,destination中的属性值会被覆盖

2、angular.toJson

  

  angular.toJson()中第二个参数为true时,代表将json形式的string格式化;

  

  格式化后更清晰。

3、angular.forEach  

  

     var values = {name: 'misko', gender: 'male'};
  var log = [];
  angular.forEach(values,function(value,key){
    console.log(value + ' ' + key);
    console.log(this);//此处this指log数组
    this.push(key + ":" + value);
  },log);
  console.log(log);

  

  在forEach中this指的是第三个参数,要将数据放入的对象。

上面的对象只需要循环一次就可以,而下面的数据则需要循环两次:

            var values2 = [
{name: 'misko', gender: 'male'},
{name: 'Amy', gender: 'female'}
];
var log2 = [];
angular.forEach(values2,function(value,key){
console.log(key);//0 1
console.log(value);//Object {name: "misko", gender: "male"} Object {name: "Amy", gender: "female"}
angular.forEach(value,function(val,k){
console.log(k);//name,gender,name,gender
console.log(val);//misko male Amy female
console.log(this);//
this.push(k + ":" + val);
},log2);//存放数据的对象要放在内部循环中
console.log(log2);//["name:misko", "gender:male", "name:Amy", "gender:female"]
});

4、angular.bind

  传递参数的方法:

    第一种:

      

var ele = {"name":"Amy"};
var bind = angular.bind(ele,function(age){
console.log(this);
console.log(this.name + ' is ' + age);
});
bind(25);

  

    第二种:

var ele = {"name":"Amy"};
var bind = angular.bind(ele,function(job){
console.log(this);
console.log(this.name + ' is ' + job);
},'a marketer');//传递参数方法二
bind();

  

5、angular.bootstrap

  angular.bootstrap手动初始化module,一个应用只能有一个ng-app,而通过angular.bootstrap可以手动启动多个module,实例如下:

    <div id="div1" ng-controller="myCtrl1">
{{name}}
</div>
<div id="div2" ng-controller="myCtrl2">
{{name}}
</div>
<script src="../angular.min.js"></script>
<script>
var myapp1 = angular.module('myApp1',[]);
var myapp2 = angular.module('myApp2',[]);
myapp1.controller('myCtrl1',['$scope',function($scope){
$scope.name="zhangsan";
}]);
myapp2.controller('myCtrl2',['$scope',function($scope){
$scope.name="lisi";
}]);
var div1 = document.getElementById('div1');
var div2 = document.getElementById('div2');
angular.element(document).ready(function(){//此页面没有使用ng-app指定任一module,而是动态添加
angular.bootstrap(div1,['myApp1']);
angular.bootstrap(div2,['myApp2'
]);
});
/*window.onload = function(){//整个页面加载完成时
angular.bootstrap(div1,['myApp1']);
angular.bootstrap(div2,['myApp2']);
}*/
</script>

  此段代码中没有指定ng-app,而是通过angular.bootstrap来手动加载module。

  模块之间相关依赖注入: 

<body ng-app="myApp">
<div ng-controller="firstCtrl">
{{name}}
</div>
<div ng-controller="secondCtrl">
{{name}}
</div>
<div ng-controller="thirdCtrl">
{{name}}
</div>
<script src="../angular.min.js"></script>
<script src="module2.js"></script>
<script>
angular.module('myApp',['myApp2'])//将myApp2注入到myApp中
.controller('firstCtrl',['$scope',function($scope){
$scope.name="zhangsan";
}]); </script>
</body>

  module2.js:js文件中定义了模块myApp2和secondCtrl及thirdCtrl

angular.module('myApp2',[])
.controller('secondCtrl',['$scope',function($scope){
$scope.name="lisi";
}])
.controller('thirdCtrl',['$scope',function($scope){
$scope.name="wangwu";
}]);

angularjs工具方法的更多相关文章

  1. angularjs——工具方法

    1.fromJson 把json字符串转成JSON对象 var jsonStr='[{"Name":"abc","age":12},{&qu ...

  2. angularjs 工具方法

    <!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" ...

  3. javascript 的工具方法 --- 类型判断

    Javascript中常见类型对象有: Boolean, Number, String, Function, Array, Date, RegExp, Object, Error, Symbol等等. ...

  4. JQuery操作类数组的工具方法

    JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...

  5. 当AngularJS POST方法碰上PHP

    问题描述 怎么POST过去给PHP都收不到资料? $_POST方法取不到正确的传入值! 原理说明 AngularJS这套framework使用的AJAX方法中,资料传递的格式为JSON,送出去的hea ...

  6. jQuery工具方法

    目录 常用工具方法 判断数据类型的方法 Ajax操作 $.ajax 简便写法 Ajax事件 返回值 JSONP 文件上传 参考链接 jQuery函数库提供了一个jQuery对象(简写为$),这个对象本 ...

  7. jQuery晦涩的底层工具方法们

    这里整理的是jQuery源码中一些比较晦涩难懂的.内部的.最底层的工具方法,它们多为jQuery的上层api方法服务,目前包括: jQuery.access jQuery.access: functi ...

  8. angular的工具方法笔记(equals, HashKey)

    分别是angular脏值检测的工具方法equals和 类HashKey的使用方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi ...

  9. JavaScript文件中调用AngularJS内部方法或改变$scope变量

    需要在其他JavaScript文件中调用AngularJS内部方法或改变$scope变量,同时还要保持双向数据绑定: 首先获取AngularJS application: 方法一:通过controll ...

随机推荐

  1. ContentProvider备份短信,以xml文件存储

    因为短信的内容已经通过ContentProvider暴露出来,所以我们可以直接用内容解析者获取短信内容. 想要获取短信内容,你需要知道的一些东西: 1.Uri uri = Uri.parse(&quo ...

  2. Ubuntu下安装支付宝安全控件

    在淘宝购物时,安装支付宝安全控件.下载了一个文件.tar.gz(非常小的一个文件). tar -zxvf 解压之,只有一个aliedit.sh文件,运行这个文件就安装成功了,重启firefox就可以用 ...

  3. SharePoint 2013 安装图解

    转自: http://www.cnblogs.com/jianyus/archive/2013/02/01/2889653.html 介绍:文章就是SharePoint2013安装过程的图解,包括步骤 ...

  4. 十天冲刺---Day4

    站立式会议 站立式会议内容总结: git上Issues新增内容: 燃尽图 照片 队伍度过了一次难关,刚开始学习的难关. 但还是存在进度较慢的问题. 队伍内相互理解是关键. 要时刻了解队友的情况.

  5. session超时设置

    session的设置方法有三种: 1.直接在应用服务器中设置 如果是Tomcat,可以在Tomcat目录下conf/web.xml中找到<session-config>元素,tomcat默 ...

  6. Elasticsearch: Indexing SQL databases. The easy way

    Elasticsearchis a great search engine, flexible, fast and fun. So how can I get started with it? Thi ...

  7. hdu2665 && poj2104划分树

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 47066   Accepted: 15743 Ca ...

  8. hdu2888 二维RMQ

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. mysql命令-use

    use命令可以让我们来使用数据库. use命令格式: use <数据库名>; 例如,如果xhkdb数据库存在,尝试存取它: mysql> use xhkdb; 屏幕提示:Databa ...

  10. Spring-程序中获取注册bean的方式

    获得spring里注册Bean的四种方法,特别是第三种方法,简单: 一:方法一(多在struts框架中)继承BaseDispatchAction  import com.mas.wawacommuni ...