angularjs 工具方法
<!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> <script> //angular.bind(); -> $.proxy() : 改this指向 function show(n1,n2){ alert(n1); alert(n2); alert(this); } angular.bind(document,show,3)(4);//改变show函数的this指向, //angular.copy(); //拷贝对象 var a = {name : 'hello'}; var b = {age : '20'}; var c = angular.copy(a,b); //a把所有值覆盖给了b console.log(b); //angular.extend(); //对象继承 var a = { name : 'hello' }; var b = { age : '20' }; var c = angular.extend(b,a); //c有 console.log(b); </script> </head> <body> </body> </html>
<!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="jquery-1.11.1.js"></script> <script src="angular.min.js"></script> <script> var a = []; console.log(angular.isArray(a));//是不是数组 window.onload = function(){ console.log(angular.isElement( document.body )); console.log(angular.isElement( $(document.body) )); }; console.log(angular.version); var a = NaN; var b = NaN; console.log(angular.equals(a,b)); var values = ['a','b','c']; var values = {'name':'hello','age':'20'}; var result = []; angular.forEach(values,function(value,i){ console.log(value); console.log(i); this.push( value + i );//this是result },result); console.log(result); //JSON.parse() JSON.stringify() var str = '{"name":"hello","age":"20"}'; var json = angular.fromJson(str); console.log(json); var json = {"name":"hello","age":"20"}; var str = angular.toJson(json,true); console.log( str ); //angular.identity/noop var str = 'hello'; console.log(angular.identity(str)); //hello function identity(str){ return str; } console.log(angular.noop()); //undefined function noop(){ } console.log(angular.uppercase('hello')); </script> </head> <body> <div id="div1">aaaaaaaa</div> <script> var oDiv = document.getElementById('div1'); $('#div1').css('background','red'); //angular.element === $ </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> <script> var m1 = angular.module('myApp1',[]); var m2 = angular.module('myApp2',[]); m1.controller('Aaa',['$scope',function($scope){//定义Aaa控制器的函数, $scope.name = 'hello'; }]); m2.controller('Bbb',['$scope',function($scope){ $scope.name = 'hi'; }]); document.onclick = function(){ var aDiv = document.getElementsByTagName('div'); angular.bootstrap(aDiv[0],['myApp1']); angular.bootstrap(aDiv[1],['myApp2']); }; </script> </head> <body> <div ng-controller="Aaa"> <p>{{name}}</p> </div> <div ng-controller="Bbb"> <p>{{name}}</p> </div> </body> </html>
<!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> <script> function Aaa($scope,$timeout){ $scope.name = 'hello'; setTimeout(function(){ //$scope.name = 'hi'; $scope.$apply(function(){//$apply针对数据变化有用 $scope.name = 'hi'; }); },2000); $timeout(function(){ $scope.name = 'hi'; },2000); $scope.show = function(){ $scope.name = 'hi'; }; } </script> </head> <body> <!--<div ng-controller="Aaa" ng-click="name='hi'">--> <div ng-controller="Aaa" ng-click="show()"> <p>{{name}}</p> </div> </body> </html>
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> <script> var m1 = angular.module('myApp',[]); /*m1.controller('Aaa',['$scope',function($scope){//控制器的函数 $scope.name = 'hello'; }]); m1.controller('Bbb',['$scope',function($scope){ $scope.name = 'hi'; }]);*/ m1.run(['$rootScope',function($rootScope){ // $rootScope.name = 'hello'; }]); console.log( m1 ); </script> </head> <body> <div> <p>{{name}}</p> </div> </body> </html>