AngularJS(一)

标准形式创建module与controller

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>standard</title>
    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>

</head>
<script>
    var myAppModule=angular.module('myApp',[]);
    myAppModule.controller('TextController',
        function($scope){
            var someText={};
            someText.message="文本信息";
            $scope.someText=someText;
        }
    );

</script>

<body ng-controller="TextController">
    <p>{{someText.message}}</p>
</body>
</html>

ng-change
$watch

<!DOCTYPE html>
<html lang="en" ng-app="estimateModule">
<head>
    <meta charset="UTF-8">
    <title>findingEstimate</title>
    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>
    <script>
        var myModule=angular.module('estimateModule',[]);

        /*myModule.controller('estimateController',
                function($scope){
                    $scope.funding={startingEstimate:0};
                    $scope.computeNeeded=function(){
                        $scope.funding.needed=$scope.funding.startingEstimate*10;
                    };

                });*/

        function StartUpController($scope){
            $scope.funding={startingEstimate:0};
            computeNeeded=function(){
                $scope.funding.needed=$scope.funding.startingEstimate*10;
            };
            $scope.$watch('funding.startingEstimate',computeNeeded);
        }
    </script>



</head>
<body>
<form ng-controller="estimateController">
    Starting: <input ng-change="computeNeeded()"
    ng-model="funding.startingEstimate"
>
    Recommendation:{{funding.needed}}
</form>

</body>
</html>

ng-submit

<!DOCTYPE html>
<html lang="en" ng-app="myModule">
<head>
    <meta charset="UTF-8">
    <title>formRequest</title>
    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>
    <script>
        var myModule=angular.module('myModule',[]);

        function formController($scope){
            $scope.computeNeeded=function(){
                $scope.needed=$scope.startingEstimate*10;
            };

            $scope.requestFunding=function(){
                window.alert("sorry you are not qualified");

            }
        }

    </script>

</head>
<body ng-controller="formController">
<form ng-submit="requestFunding()">
    Starting: <input ng-change="computeNeeded()" ng-model="startingEstimate">
    Recommendation:{{needed}}
    <button>Fund my start up</button>

</form>
</body>
</html>

reset

<!DOCTYPE html>
<html lang="en" ng-app="MyModule">
<head>
    <meta charset="UTF-8">
    <title>reset</title>
</head>
<script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>
<script>
    var myModule=angular.module('MyModule',[]);

    myModule.controller('formController',
        function($scope){
            $scope.requestFunding=function(){
                window.alert("sorry your money is not enough!")
            };
            $scope.computeNeeded=function(){
                $scope.needed=$scope.startingEstimate*10;
            };
            $scope.reset=function(){
                $scope.startingEstimate=0;
                $scope.needed=0;
            };
            $scope.alert=function(){
                window.alert("钱不够!");
            }

        }
    );

</script>


<body>
<form ng-controller="formController" ng-submit="requestFunding()">
    starting: <input ng-model="startingEstimate" ng-change="computeNeeded()">
    Recommendation:{{needed}}
    <button ng-click="reset()">reset</button>
    <button ng-click="alert()">fundMyStartUp</button>

</form>
</body>
</html>

ng-repeat

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>repeat</title>

    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>

    <script>
        var students=[
            {name:'jack',id:'1'},
            {name:'tom',id:'2'},
            {name:'jerry',id:'2'},
        ]

        function stuController($scope){
            $scope.studentList=students;
        }
    </script>
</head>
<body ng-controller="stuController">
<ul>

    <li ng-repeat="s in studentList">
        <a href="#">{{s.name}}</a>
    </li>

</ul>

</body>
</html>
$first  $middle $last
<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>index</title>
    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>
    <script>
        var album=[
            {name:'south',duration:'2:20'},
            {name:'north',duration:'3:20'},
            {name:'west',duration:'4:20'}
        ]

        function AlbumController($scope){
            $scope.album=album;
        }

    </script>
</head>
<body ng-controller="AlbumController">
<table>
    <tr ng-repeat="a in album">
        <td>{{$index+1 }}</td>
        <td>{{a.name }}</td>
        <td>{{a.duration }}</td>
    </tr>
</table>

</body>
</html>

ng-show
ng-hide

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>showhide</title>
    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>
    <script>
        function showHideController($scope){
            $scope.menuState={show:true};
            $scope.toggleMenu=function(){
                $scope.menuState.show=!$scope.menuState.show;
            }
        }
    </script>


</head>
<body>
<div ng-controller="showHideController">
    <button ng-click="toggleMenu()">显示/隐藏</button>
        <ul ng-show="menuState.show">
            <li ng-click="stun()">Stun</li>
            <li ng-click="disIntegrate()">DisIntegrate</li>
            <li ng-click="erase()">Erase from history</li>
        </ul>
</div>
</body>
</html>

通过双向数据绑定来控制CSS样式

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>cssControl</title>
    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>

    <style type="text/css">
        .menu-disable-true{
            color:gray;
        }
    </style>

    <script>
        function cssController($scope){
            $scope.isDisabled=false;
            $scope.stun=function(){
                $scope.isDisabled=true;
            }

        }

    </script>
</head>
<body>
<div ng-controller="cssController">
    <ul>
        <li class="menu-disable-{{isDisabled}}">Stun</li>
        <button ng-click="stun()">disable</button>
    </ul>
</div>

</body>
</html>

error warning

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>errorWarning</title>
    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>

    <style type="text/css">
        .error{
            background-color: red;
        }
        .warning{
            background-color: green;
        }
    </style>

    <script>
        function HeaderController($scope){
            $scope.isError=false;
            $scope.isWarning=false;
            $scope.showError=function(){
                $scope.isError=true;
                $scope.isWarning=false;
                $scope.messageText="这是错误的文本内容";
            }
            $scope.showWarning=function(){
                $scope.isWarning=true;
                $scope.isError=false;
                $scope.messageText="这是警告的文本信息"
            }

        }

    </script>
</head>
<body>
    <div ng-controller="HeaderController">
        <div ng-class="{error:isError,warning:isWarning}">{{messageText}}</div>

        <button ng-click="showError()">SimulateError</button>
        <button ng-click="showWarning()">SimulateWarning</button>

    </div>
</body>
</html>

选中后改变该行的颜色

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>selected</title>

    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>

    <style type="text/css">
        .selected{
            background-color: lightgreen;
        }
    </style>

    <script>
        function restaurantController($scope){
            $scope.directory=[
                {name:'hello',place:'china'},
                {name:'world',place:'japan'},
                {name:'kitty',place:'UK'}
            ];

            $scope.selectRestaurant=function(row){
                $scope.selectedRow=row;
            }


        }

    </script>
</head>
<body>
    <table ng-controller="restaurantController">
        <tr ng-repeat="r in directory" ng-click="selectRestaurant($index)"
            ng-class="{selected:$index==selectedRow}"
        >
            <td>{{r.name}}</td>
            <td>{{r.place}}</td>
        </tr>
    </table>


</body>
</html>

ng-src
ng-href

控制器在应用中的三个作用
2.在应用模型中设置初始状态
3.通过$scope向视图暴露模型和函数
4.监视模型发生变化的其他部分并做出相应的动作



通过表达式发布模型数据

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>setCount</title>
    <script src="http://lib.sinaapp.com/js/angular.js/angular-1.2.19/angular.min.js"></script>


    <script>
        function countController($scope){
            $scope.count=1;
            $scope.setCount=function(){
                $scope.count=3;
            };
        }
    </script>

</head>
<body>
    <div ng-controller="countController">
        <button ng-click="setCount()">set count three</button>
        <button ng-click='count=4'>set count four</button>
        <p>{{count}}</p>
    </div>


</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值