angular 复习

作者: xinlei87 | 来源:发表于2018-04-05 15:00 被阅读0次

    重复一个html元素:

    <ul>
    <li ng-repeat="x in names">
    {{x}}
    </li>
    </ul>
    

    验证用户输入

    <form ng-app="" name="myForm" ng-init="myText = 'test@runoob.com'">
        Email:
        <input type="email" name="myAddress" ng-model="myText" required></p>
        <h1>状态</h1>
        {{myForm.myAddress.$valid}}
        {{myForm.myAddress.$dirty}}
        {{myForm.myAddress.$touched}}
    </form>
    

    所有的应用都有一个$rootscope,它可以作用在ng-app指令包含的所有html元素中。是各个controller中scope的桥梁。用rootscope定义的值,可以在各个controller中使用。

    $http是angularjs中的有一个核心服务,用于读取远程服务器的数据。使用格式:

    
    var app = angular.module('myApp', []);
        
    app.controller('siteCtrl', function($scope, $http) {
        $http({
            method: 'GET',
            url: 'https://www.runoob.com/try/angularjs/data/sites.php'
        }).then(function successCallback(response) {
                $scope.names = response.data.sites;
            }, function errorCallback(response) {
                // 请求失败执行代码
        });
      
    });
    
    

    使用ng-option指令来创建一个下拉菜单

    
    <div ng-app="myApp" ng-controller="myCtrl">
     
    <select ng-init="selectedName = names[0]" ng-model="selectedName" ng-options="x for x in names">
    </select>
     输入验证:
    

    <form ng-app="myApp" ng-controller="validateCtrl"
    name="myForm" novalidate>

    <p>用户名:

    <input type="text" name="user" ng-model="user" required>
    <span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
    <span ng-show="myForm.user.$error.required">用户名是必须的。</span>
    </span>
    </p>

    <p>邮箱:

    <input type="email" name="email" ng-model="email" required>
    <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
    <span ng-show="myForm.email.$error.required">邮箱是必须的。</span>
    <span ng-show="myForm.email.$error.email">非法的邮箱。</span>
    </span>
    </p>

    <p>
    <input type="submit"
    ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
    myForm.email.$dirty && myForm.email.$invalid">
    </p>

    </form>

    <script>
    var app = angular.module('myApp', []);
    app.controller('validateCtrl', function($scope) {
    $scope.user = 'John Doe';
    $scope.email = 'john.doe@gmail.com';
    });
    </script>

    </div>
     
    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.names = ["Google", "Runoob", "Taobao"];
    });
    </script>
    
    

    表格

    <table>
      <tr ng-repeat="x in names">
        <td>{{ x.Name }}</td>
        <td>{{ x.Country }}</td>
      </tr>
    </table>
     
    </div>
     
    <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope, $http) {
        $http.get("/try/angularjs/data/Customers_JSON.php")
        .then(function (result) {
            $scope.names = result.data.records;
        });
    });
    </script>
    
    

    angular的复选框,单选框以及数据绑定

    
    <form>
        Check to show a header:
        <input type="checkbox" ng-model="myVar">
    </form>
     
    <h1 ng-show="myVar">My Header</h1>
    
    
    <form>
        选择一个选项:
        <input type="radio" ng-model="myVar" value="dogs">Dogs
        <input type="radio" ng-model="myVar" value="tuts">Tutorials
        <input type="radio" ng-model="myVar" value="cars">Cars
    </form>
    

    页面切换:ng-route
    angular路由云需我们通过不童的url访问不同的内容通过angular可以实现但也多视图的应用
    例如:

    <html>
        <head>
            <meta charset="utf-8">
            <title>AngularJS 路由实例 - 菜鸟教程</title>
        </head>
        <body ng-app='routingDemoApp'>
         
            <h2>AngularJS 路由应用</h2>
            <ul>
                <li><a href="#/">首页</a></li>
                <li><a href="#/computers">电脑</a></li>
                <li><a href="#/printers">打印机</a></li>
                <li><a href="#/blabla">其他</a></li>
            </ul>
             
            <div ng-view></div>
            <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
            <script src="http://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script>
            <script>
                angular.module('routingDemoApp',['ngRoute'])
                .config(['$routeProvider', function($routeProvider){
                    $routeProvider
                    .when('/',{template:'这是首页页面'})
                    .when('/computers',{template:'这是电脑分类页面'})
                    .when('/printers',{template:'这是打印机页面'})
                    .otherwise({redirectTo:'/'});
                }]);
            </script>
         
         
        </body>
    </html>
    

    angular的弹窗

        <!DOCTYPE html>  
        <html ng-app="ModalDemo">  
        <head>  
        <title></title>  
        <link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">  
        <script src="lib/angular/angular.min.js"></script>  
        <script src="lib/bootstrap-gh-pages/ui-bootstrap-tpls-0.7.0.min.js"></script>  
        <script src="lib/angular/i18n/angular-locale_zh-cn.js"></script>  
        </head>  
        <body>  
            <div ng-controller="ModalDemoCtrl">  
                <script type="text/ng-template" id="myModalContent.html" />  
                <div class="modal-header">  
                    <h3>I'm a modal!</h3>  
                </div>  
                <div class="modal-body">  
                    <ul>  
                        <li ng-repeat="item in items"><a  
                            ng-click="selected.item = item">{{ item }}</a></li>  
                    </ul>  
                    Selected: <b>{{ selected.item }}</b>  
                </div>  
                <div class="modal-footer">  
                    <button class="btn btn-primary" ng-click="ok()">OK</button>  
                    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>  
                </div>  
                </script>  
                <button class="btn" ng-click="open()">Open me!</button>  
            </div>  
            <script>  
                var ModalDemo = angular.module('ModalDemo', [ 'ui.bootstrap' ]);  
                var ModalDemoCtrl = function($scope, $modal, $log) {  
                    $scope.items = [ 'item1', 'item2', 'item3' ];  
                    $scope.open = function() {  
                        var modalInstance = $modal.open({  
                            templateUrl : 'myModalContent.html',  
                            controller : ModalInstanceCtrl,  
                            resolve : {  
                                items : function() {  
                                    return $scope.items;  
                                }  
                            }  
                        });  
                        modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
                            console.log('modal is opened');  
                        });  
                        modalInstance.result.then(function(result) {  
                            console.log(result);  
                        }, function(reason) {  
                            console.log(reason);// 点击空白区域,总会输出backdrop  
                            // click,点击取消,则会暑促cancel  
                            $log.info('Modal dismissed at: ' + new Date());  
                        });  
                    };  
                };  
                var ModalInstanceCtrl = function($scope, $modalInstance, items) {  
                    $scope.items = items;  
                    $scope.selected = {  
                        item : $scope.items[0]  
                    };  
                    $scope.ok = function() {  
                        $modalInstance.close($scope.selected);  
                    };  
                    $scope.cancel = function() {  
                        $modalInstance.dismiss('cancel');  
                    };  
                };  
            </script>  
        </body>  
        </html>  
    

    相关文章

      网友评论

        本文标题:angular 复习

        本文链接:https://www.haomeiwen.com/subject/hyyrhftx.html