• AngularJS -- 代码实例


    整理书籍内容(QQ:283125476 发布者:M 【重在分享,有建议请联系-》QQ号】)

    ng-change 当文本输入字段中内容发生了变化,就会改变equation.x的值:

    <body ng-app="myApp">
        <div ng-controller="TestController">
            <input type="text" 
                ng-model="equation.x"
                ng-change="change()" />
            <code>{{ equation.output }}</code>
        </div>
    
    
        <script>
        var app = angular.module("myApp", []);
    
        app.controller("TestController", function($scope) {
            $scope.equation = {};
            $scope.change = function() {
                $scope.equation.output = parseInt($scope.equation.x) + 2;
            };
        });
        </script>
    </body>
    
    • 表单合法时设置 ng-valid;
    • 表单不合法时设置 ng-invlid;
    • 表单未进行修改时设置 ng-pristion;
    • 表单进行过修改时设置 ng-dirty;

    ng-cloak效果跟ng-bind差不多,就是为了防止闪烁

    <body ng-app="myApp">
        <div ng-controller="TestController">
            <p ng-cloak>sss{{ aaa }}</p>
        </div>
        
        <script>
        var app = angular.module("myApp", []); 
    
        app.controller("TestController", function($scope, $timeout) {
            //dosomething;
            $timeout(function() {
                $scope.aaa = "lll";
            }, 1000);
        });
        </script>
    </body>
    

    ng-click

    <body ng-app="myApp">
        <div ng-controller="TestController">
            <button ng-click="count = count +1"
                    ng-init="count = 9">increment</button>
    
            count: {{ count }};
            <button ng-click="decrement()">decrement</button>
        </div>
    
        <script>
        var app = angular.module("myApp", []); 
    
        app.controller("TestController", function($scope) {
            //dosomething;
            $scope.decrement = function() {
                $scope.count = $scope.count - 1;
            };
        });
        </script>
    </body>
    

    ng-select

    <body ng-app="myApp">
        <div ng-controller="TestController">
            <ul>
                <li ng-repeat="color in colors">
                  Name: <input ng-model="color.name">
                  [<a href ng-click="colors.splice($index, 1)">X</a>]
                </li>
                <li>
                  [<a href ng-click="colors.push({})">add</a>]
                </li>
              </ul>
              <hr/>
              Color (null not allowed):
              <select ng-model="myColor" ng-options="color.name for color in colors"></select><br>
    
              Color (null allowed):
              <span  class="nullable">
                <select ng-model="myColor" ng-options="color.name for color in colors">
                  <option value="">-- choose color --</option>
                </select>
              </span><br/>
    
              Color grouped by shade:
              <select ng-model="myColor" ng-options="color.name group by color.shade for color in colors">
              </select><br/>
    
    
              Select <a href ng-click="myColor = { name:'not in list', shade: 'other' }">bogus</a>.<br>
              <hr/>
              Currently selected: {{ {selected_color:myColor} }}
              <div style="border:solid 1px black; height:20px"
                   ng-style="{'background-color':myColor.name}">
              </div>
        </div>
    
        <script>
        var app = angular.module("myApp", []); 
    
        app.controller("TestController", function($scope) {
            $scope.colors = [
                  {name:'black', shade:'dark'},
                  {name:'white', shade:'light'},
                  {name:'red', shade:'dark'},
                  {name:'blue', shade:'dark'},
                  {name:'yellow', shade:'light'}
                ];
                $scope.myColor = $scope.colors[2]; 
        });
        </script>
    </body>
    

    ng-attr-(suffix)

    当AngularJS编译DOM时会查找花括号{{ some expression }}内的表达式。这些表达式会被自动注册到$watch服务中
    并更新到$disgest循环中,成为它的一部分:

    有时候浏览器会对属性进行很苛刻的限制。SVG就是一个例子:

    <svg>
       <circle cx="{{ cs }}"></sircle>
    </svg>
    

    运行上面的代码会抛出一个错误,指出我们有一个非法属性,可以用ng-attr-cs来解决这个问题,
    注意,cx位于这个名称的尾部,在这个属性中,通过用来{{ }}写表达式,达到前面提到的目的:

    <svg>
        <circle ng-attr-cx="{{ cx }}"></circle>
    </svg>
    
    通过分享,结交好友~ 如本文对您有益,请给予关注。转载请注明出处!-- 小数
  • 相关阅读:
    asp.net调用mysql 存储过程 带 out 返回值,返回刚插入数据库中的自增的ID,LAST_INSERT_ID() 的使用
    如何俘获一个 IT 男的心,让他成为男友然后变成老公?
    MySqlHelper.cs mysql数据库助手类
    mysql 按年度、季度、月度、周、日SQL统计查询,mysql 存储过程 中 in 和 FIND_IN_SET 传递多个参数的使用
    奇怪的母版页里面的 form 表单里面的 enctype="multipart/formdata" html控件上传 FileUpload控件上传 一次多图片上传
    asp.net 连接 Mysql 代码生成器(下载地址)
    Convert.ToInt32、(int)和int.Parse,int.TryParse四者之间的区别:
    在web项目中 使用 WebService 根据IP地址来源搜索实际物理地址,常用的WebServices
    vc6控制台程序利用SoapToolkit3.0调用WebService
    浅议C++/CLI的gcnew关键字
  • 原文地址:https://www.cnblogs.com/mcat/p/4496787.html
Copyright © 2020-2023  润新知