AngularJs-07-指令

简介:
<!DOCTYPE html>
<html lang="zh" ng-app="myapp">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <script src="../bower_components/angular/angular.js" type="text/javascript" charset="utf-8"></script>
    <title>AngularJs-指令</title>
    
    <style type="text/css">
        .red{color:red};
        .green{color:green};
    </style>
</head>
<body>
    <h3>AngularJs-$watch-01</h3>
    <input type="text" ng-model="username" />
    {{username}}
    <p>改变的次数为 : {{count}}</p>

    <h3>AngularJs-$watch-02</h3>

    <div ng-repeat="item in items">
        <input type="text" ng-model="item.value" />{{item.value}}
    </div>
    <p>改变的次数为 : {{rcount}}</p>

    <h3>AngularJs-$watchCollection</h3>

    <a href="javascript:void(0);" ng-click="addvalue()">add</a>
    <a href="javascript:void(0);" ng-click="delvalue()">del</a>
    <a href="javascript:void(0);" ng-click="unwatch()">unwatch</a>
    
    <div ng-repeat="item in items">
        <input type="text" ng-model="item.value"/>{{item.value}}
    </div>
    
    
    <h3>AngularJs-ng-switch</h3>
    <!--不可见-->
    <div ng-controller="ifController">
        
        <div ng-switch on="age">
            <span ng-switch-when="10">青年1</span>
            <span ng-switch-when="20">青年2</span>
            <span ng-switch-when="30">青年3</span>
            <span ng-switch-when="40">青年4</span>
            <span ng-switch-default>青年5</span>
        </div>
    
        
        <h3>AngularJs-ng-if</h3>
        <div>
            <span ng-if="age>=60 && age<80">老年</span>
            <span ng-if="age>=40 && age<60">壮年</span>
            <span ng-if="age>=30 && age<40">青年</span>
            <span ng-if="age<10 && age<30">少年</span>
        </div>
    </div>
    
    <script type="text/javascript">
        var app = angular.module("myapp",[]);
        
        // run主要作用为代表angularjs装在完毕某一些内置对象的时候
        // 可能会执行以下回调函数,回调就初始化代码.
        
        /*app.run(function($rootScope){
            $rootScope.username = "bilaisheng";
        });*/
        
        app.run(["$rootScope", "$log", function($rootScope, $log){
            $rootScope.username="bilaisheng";
            $rootScope.count = 0;
            
            $rootScope.$watch("username",function(){
                $rootScope.count++;
            });
        }]);
        
        app.run(["$rootScope","$log",function($rootScope,$log){
            $rootScope.items = [
                {value:1},
                {value:2},
                {value:3},
                {value:4}
            ];
            
            $rootScope.rcount = 0;
            $rootScope.$watch("items", function(){
                $rootScope.rcount++;
            },true);// 为fasle: "引用监视". true: 全等监视
        
        
            // watchCollection
            $rootScope.wcount = 0;
            var unWatcher = $rootScope.$watchCollection("items",function(){
                $rootScope.wcount++;
            });
        
        }]);
        
        app.run(["$rootScope","$log",function($rootScope,$log){
                
            $rootScope.items = [
                {value:1},
                {value:2},
                {value:3},
                {value:4}
            ];
            
            //$watchCollection 方法用来针对数组或者集合进行监视,他的性能介于全等监视和引用监视之间
            //但是它并不会对数组中的每一项的属性进行监视,但是可以对数组的项目增减做出反应,
            
            $rootScope.count = 0;
            var unWatcher = $rootScope.$watchCollection("items",function(){
                $rootScope.count++;
            });
            
            //添加值
            $rootScope.addvalue = function(){
                $rootScope.items.push({value:5});
            };
            
            //删除值
            $rootScope.delvalue = function(){
                $rootScope.items.splice(0,1);
            };
            
            //删除监听
            $rootScope.unwatch = function(){
                unWatcher();//解除监听
            };
        }]);
        
        /**
        * 1: 选中的元素:
        *     checkbox radio
        *         ng-checked = true/false
        *     select
        *         ng-selected = true/false
        *     禁止选择:
        *        ng-disabled=true/false
        *    2:关于指令如果调用作用域属性和方法的时候是不需要表达式的
        *
        */
        app.controller("ifController",function($scope){
            $scope.age = 30;
        });
    </script>
    
</body>
</html>
目录
相关文章
|
10月前
|
移动开发 前端开发 JavaScript
AngularJS 技术深入解析
AngularJS 是一个流行的 JavaScript 框架,用于构建动态的单页面应用程序(SPA)。它提供了一种优雅而强大的方式来开发前端应用,具有出色的数据绑定、模块化和可扩展性等特性。本文将深入探讨 AngularJS 的一些关键技术。
80 0
|
JavaScript 内存技术
angularJS 常用指令小结
angularJS 常用指令小结
129 0
|
JavaScript
AngularJs错误
AngularJs错误
72 0
|
JavaScript 前端开发 移动开发
|
JavaScript 前端开发 容器
|
索引 图形学
|
Web App开发 JavaScript 前端开发