夺命雷公狗—angularjs—25—angular内置的方法(高级)

查看版本信息

angular.version

 

 

 

console.log(angular.version);

 

 

 

判断是否相等

angular.equals()

 

 

 

    var str1 = '';
    var str2 = '';
    console.log(angular.equals(str1,str2));
    var arr1 = [];
    var arr2 = [];
    console.log(angular.equals(arr1,arr2));
    var n1 = NaN;
    var n2 = NaN;
    console.log(angular.equals(n1,n2));

 

 

 

 

forEach遍历

angular.forEach

 

 

 

<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/angular.min.js"></script>
</head>
<body>
</body>
<script>
    var arr = ['aaa','bbb','ccc'];
    angular.forEach(arr,function(val,i){
        console.log(val+'---------'+i);
    });
    console.log('-------------------------------');

    var objj = { "name":"lisi","age":"80","sex":'1' }
    angular.forEach(objj,function(val,i){
        console.log(i+'------'+val);
    })

    var objj2 = { "name":"lisi","age":"80","sex":'1' }
    var result = [];
    angular.forEach(objj2,function(val,i){
        this.push(i+'---'+val);
    },result);
    console.log(result);
</script>
</html>

 

 

 

 

 

fromJson  和  toJson   

 

<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/angular.min.js"></script>
</head>
<body>
</body>
<script>
    //源生的JSON.parse(),json转字符串     JSON.stringify()字符串转json
    //angular里面的:
    //angular.fromJson[str]            字符串转json
    //angular.toJson[json,[true]]      json转字符串
    var str = '{"name":"lisi","age":"50"}';
    var json = angular.fromJson(str);
    console.log(json);

    var str2 = {"name":"lisi","age":"50"};
    var json2 = angular.toJson(str2);
    console.log(json2);

    var json3 = angular.toJson(str2,true);  //加true即可实现格式化显示输出
    console.log(json3);

</script>
</html>

 

 

 

 


 

 

angular里面也是封装了一套有点类似jquery里面的方法:

element

 

 

<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/angular.min.js"></script>
</head>
<body>
    <div id="dd">aaa</div>
</body>
<script>
    var dd = document.getElementById('dd');
    //angularjs里面封装了一套类似jquery的方法
    angular.element(dd).css('background','red');

</script>
</html>

 

 

 

我们在angularjs 的官网上可以看到jquery的方法的确是封装了不少的。。。

https://docs.angularjs.org/api/ng/function/angular.element

 

 

 

 

 

run方法这样可以实现少点控制器,但坏处是值都挂载在全局作用域下的,如挂载在局部作用域下则会报错

 

<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/angular.min.js"></script>
</head>
<body>
    <div>{{name}}</div>
</body>
<script>
    var app = angular.module('myapp',[]);
    app.run(['$rootScope',function($rootScope){
        $rootScope.name = 'hello';
    }]);
</script>
</html>

 

 

 

posted @ 2016-08-24 16:07  夺命雷公狗  阅读(740)  评论(0编辑  收藏  举报