最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular中的$watch、$watchGroup、$watchCollection

 更新時間:2017年06月25日 08:46:46   作者:webNick  
這篇文章主要介紹了Angular中的$watch、$watchGroup、$watchCollection ,需要的朋友可以參考下

•  1,原型:$watch: function(watchExp, listener, objectEquality, prettyPrintExpression){};

•  2,參數(shù):watchExp(必須):{(function()|string)},可以字符串表達(dá)式,也可以帶當(dāng)前scope為參數(shù)的函數(shù)

•    - `string`: Evaluated as {@link guide/expression expression}

•    - `function(scope)`: called with current `scope` as a parameter.

• 3,參數(shù):listener(必須):function(newVal, oldVal, scope),觀察的表達(dá)式變化的時候調(diào)用的函數(shù)。

• 4,參數(shù):objectEquality(非必須):是否監(jiān)視個對象,默認(rèn)為false

• 5,$scope.$digest().會執(zhí)行所有的同$scope下的$watch。

•     但會出錯$apply already in progress,換了$rootScope也一樣。

•     原因-參考大牛博客:http://blog.csdn.net/aitangyong/article/details/48972643

•     $digest、$apply、$$phase這些屬性或者方法其實都是$scope中的私有的,最好不要使用。

• 6,$watch一個對象。

•    如果要監(jiān)視對象的變化(地址改變),$watch對象名,第三個參數(shù)默認(rèn);

•    如果監(jiān)測對象中某一屬性,可以寫user.name的形式,第三個參數(shù)默認(rèn);

•     如果監(jiān)測對象中全部屬性,$watch對象名,第三個參數(shù)true;

• 7,$watchGroup,第一個參數(shù)是一個表達(dá)式的數(shù)組或者返回表達(dá)式的數(shù)組的函數(shù)。

• 8,$watchCollection;

•     js中數(shù)組也是對象,但按照$watch一個對象的方式,只有數(shù)組引用變了才能監(jiān)聽變化,增加刪除$watch監(jiān)聽不到,所以就有了$watchCollection。

•     function(obj, listener):第一個參數(shù)必須對象或者返回對象的函數(shù)。

•9,注銷$watch

•         $watch函數(shù)返回一個注銷監(jiān)聽的函數(shù),太多的$watch將會導(dǎo)致性能問題,$watch如果不再使用,我們最好將其釋放掉。

一、使用方法

html

<div ng-controller="ctrl">
    <h2>$watch</h2>
    <div>
      <input type="text" ng-model="value1"/>
    </div>
    <div ng-bind="w1"></div>
    <h2>$watchGroup</h2>
    <div>
      <input type="text" ng-model="value2"/>
      <input type="text" ng-model="value3"/>
    </div>
    <div ng-bind="w2"></div>
    <h2>$watchCollection</h2>
    <ul>
      <li ng-repeat="v in arr" ng-bind="v"></li>
    </ul>
    <div ng-bind="w3"></div>
  </div>

js

 angular.module('nickApp', [])
        .controller("ctrl", ["$scope", "$timeout", function ($scope, $timeout) {
          // $watch
          var watcher = $scope.$watch("value1", function (newVal, oldVal) {
            $scope.w1 = "$watch--" + "new:" + newVal + ";" + "old:" + oldVal;
            if (newVal == 'clear') {//設(shè)置一個注銷監(jiān)聽的條件
              watcher(); //注銷監(jiān)聽
            }
          });
          // $watchGroup
          $scope.$watchGroup(["value2", "value3"], function (newVal, oldVal) {
            //注意:newVal與oldVal都返回的是一個數(shù)組
            $scope.w2 = "$watchGroup--" + "new:" + newVal + ";" + "old:" + oldVal;
          });
          //  $watchCollection
          $scope.arr = ['nick', 'ljy', 'ljj', 'zhw'];
          $scope.$watchCollection('arr', function (newVal, oldVal) {
            $scope.w3 = "$watchCollection--" + "new:" + newVal + ";" + "old:" + oldVal;
          });
          $timeout(function () {
            $scope.arr = ['my', 'name', 'is', 'nick'];
          }, 2000);
        }])

二、小案例

html

<h2>小案例</h2>
  <ul>
    <li ng-repeat="item in items.goodsArr">
      <p ng-bind="item.goods"></p>
      <p>
        <span>單價:</span>
        <span ng-bind="item.price"></span>
      </p>
      <div>
        <input type="number" ng-model="item.num">
        <span>個</span>
      </div>
    </li>
  </ul>
  <div>
    <span>總計:</span>
    <span ng-bind="items.sum"></span>
    <span>元</span>
  </div>

js          

 //     小案例
        .factory('watchService', [function () {
          var items = {
            goodsArr: [{
              goods: 'goods1',
              price: 10,
              num: ''
            }, {
              goods: 'goods2',
              price: 20,
              num: ''
            }],
            sum: 0
          };
          return {
            getItemsSave: function () {
              return items;
            }
          };
        }])
        .controller('bodyCtl', ['$scope', 'watchService', function ($scope, watchService) {
          $scope.items = watchService.getItemsSave();
//          這里要監(jiān)聽數(shù)量變化計算綜合
          //一 只監(jiān)聽所有num變化計算總額
          var watchArr = [];
          $scope.items.goodsArr.forEach(function (v, i) {
            watchArr.push("items.goodsArr[" + i + "]['num']");
          });
          $scope.$watchGroup(watchArr, function (newVal, oldVal) { //注意:newVal與oldVal都返回的是一個數(shù)組
            $scope.items.sum = 0;
            $scope.items.goodsArr.forEach(function (v, i) {
              $scope.items.sum += v.price * (v.num > 0 ? v.num : 0);
            });
          });
/*
          //二 這樣寫則監(jiān)聽items.goodsArr所有成員
          $scope.$watch('items.goodsArr', function () {
            $scope.items.sum = 0;
            $scope.items.goodsArr.forEach(function (v, i) {
              $scope.items.sum += v.price * (v.num > 0 ? v.num : 0);
            });
          }, true);*/
        }])

全部代碼

<!DOCTYPE html>
<html ng-app="nickApp">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <title>angular之$watch、$watchGroup、$watchCollection</title>
  <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
  <script>
    /*
     * 1,原型:$watch: function(watchExp, listener, objectEquality, prettyPrintExpression){};
     * 2,參數(shù):watchExp(必須):{(function()|string)},可以字符串表達(dá)式,也可以帶當(dāng)前scope為參數(shù)的函數(shù)
     *    - `string`: Evaluated as {@link guide/expression expression}
     *    - `function(scope)`: called with current `scope` as a parameter.
     * 3,參數(shù):listener(必須):function(newVal, oldVal, scope),觀察的表達(dá)式變化的時候調(diào)用的函數(shù)。
     * 4,參數(shù):objectEquality(非必須):是否監(jiān)視個對象,默認(rèn)為false
     * 5,$scope.$digest().會執(zhí)行所有的同$scope下的$watch。
     *  但會出錯$apply already in progress,換了$rootScope也一樣。
     *  原因-參考大牛博客:http://blog.csdn.net/aitangyong/article/details/48972643
     *  $digest、$apply、$$phase這些屬性或者方法其實都是$scope中的私有的,最好不要使用。
     * 6,$watch一個對象。
     *  如果要監(jiān)視對象的變化(地址改變),$watch對象名,第三個參數(shù)默認(rèn);
     *  如果監(jiān)測對象中某一屬性,可以寫user.name的形式,第三個參數(shù)默認(rèn);
     *  如果監(jiān)測對象中全部屬性,$watch對象名,第三個參數(shù)true;
     * 7,$watchGroup,第一個參數(shù)是一個表達(dá)式的數(shù)組或者返回表達(dá)式的數(shù)組的函數(shù)。
     * 8,$watchCollection;
     *   js中數(shù)組也是對象,但按照$watch一個對象的方式,只有數(shù)組引用變了才能監(jiān)聽變化,增加刪除$watch監(jiān)聽不到,所以就有了$watchCollection。
     *   function(obj, listener):第一個參數(shù)必須對象或者返回對象的函數(shù)。
     */
    angular.module('nickApp', [])
        .controller("ctrl", ["$scope", "$timeout", function ($scope, $timeout) {
          // $watch
          var watcher = $scope.$watch("value1", function (newVal, oldVal) {
            $scope.w1 = "$watch--" + "new:" + newVal + ";" + "old:" + oldVal;
            /*
             *注銷$watch
             *太多的$watch將會導(dǎo)致性能問題,$watch如果不再使用,我們最好將其釋放掉。
             *$watch函數(shù)返回一個注銷監(jiān)聽的函數(shù),如果我們想監(jiān)控一個屬性,然后在稍后注銷它,可以使用下面的方式:
             */
            if (newVal == 'clear') {//設(shè)置一個注銷監(jiān)聽的條件
              watcher(); //注銷監(jiān)聽
            }
          });
          // $watchGroup
          $scope.$watchGroup(["value2", "value3"], function (newVal, oldVal) {
            //注意:newVal與oldVal都返回的是一個數(shù)組
            $scope.w2 = "$watchGroup--" + "new:" + newVal + ";" + "old:" + oldVal;
          });
          //  $watchCollection
          $scope.arr = ['nick', 'ljy', 'ljj', 'zhw'];
          $scope.$watchCollection('arr', function (newVal, oldVal) {
            $scope.w3 = "$watchCollection--" + "new:" + newVal + ";" + "old:" + oldVal;
          });
          $timeout(function () {
            $scope.arr = ['my', 'name', 'is', 'nick'];
          }, 2000);
        }])
        //     小案例
        .factory('watchService', [function () {
          var items = {
            goodsArr: [{
              goods: 'goods1',
              price: 10,
              num: ''
            }, {
              goods: 'goods2',
              price: 20,
              num: ''
            }],
            sum: 0
          };
          return {
            getItemsSave: function () {
              return items;
            }
          };
        }])
        .controller('bodyCtl', ['$scope', 'watchService', function ($scope, watchService) {
          $scope.items = watchService.getItemsSave();
//          這里要監(jiān)聽數(shù)量變化計算綜合
          //一 只監(jiān)聽所有num變化計算總額
          var watchArr = [];
          $scope.items.goodsArr.forEach(function (v, i) {
            watchArr.push("items.goodsArr[" + i + "]['num']");
          });
          $scope.$watchGroup(watchArr, function (newVal, oldVal) { //注意:newVal與oldVal都返回的是一個數(shù)組
            $scope.items.sum = 0;
            $scope.items.goodsArr.forEach(function (v, i) {
              $scope.items.sum += v.price * (v.num > 0 ? v.num : 0);
            });
          });
/*
          //二 這樣寫則監(jiān)聽items.goodsArr所有成員
          $scope.$watch('items.goodsArr', function () {
            $scope.items.sum = 0;
            $scope.items.goodsArr.forEach(function (v, i) {
              $scope.items.sum += v.price * (v.num > 0 ? v.num : 0);
            });
          }, true);*/
        }])
  </script>
</head>
<body ng-controller="bodyCtl">
<div ng-view>
  <div ng-controller="ctrl">
    <h2>$watch</h2>
    <div>
      <input type="text" ng-model="value1"/>
    </div>
    <div ng-bind="w1"></div>
    <h2>$watchGroup</h2>
    <div>
      <input type="text" ng-model="value2"/>
      <input type="text" ng-model="value3"/>
    </div>
    <div ng-bind="w2"></div>
    <h2>$watchCollection</h2>
    <ul>
      <li ng-repeat="v in arr" ng-bind="v"></li>
    </ul>
    <div ng-bind="w3"></div>
  </div>
  <h2>小案例</h2>
  <ul>
    <li ng-repeat="item in items.goodsArr">
      <p ng-bind="item.goods"></p>
      <p>
        <span>單價:</span>
        <span ng-bind="item.price"></span>
      </p>
      <div>
        <input type="number" ng-model="item.num">
        <span>個</span>
      </div>
    </li>
  </ul>
  <div>
    <span>總計:</span>
    <span ng-bind="items.sum"></span>
    <span>元</span>
  </div>
</div>
</body>
</html>

以上所述是小編給大家介紹的Angular中的$watch、$watchGroup、$watchCollection,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

相關(guān)文章

最新評論

萍乡市| 蛟河市| 介休市| 凭祥市| 吉隆县| 达州市| 克什克腾旗| 元氏县| 阿鲁科尔沁旗| 仁化县| 揭西县| 马山县| 吉水县| 汪清县| 南安市| 手游| 股票| 柘城县| 威海市| 伊通| 兖州市| 正蓝旗| 镇宁| 云阳县| 公安县| 高阳县| 甘孜县| 布尔津县| 沛县| 江华| 乌拉特后旗| 福贡县| 高阳县| 沅陵县| 铜梁县| 嵩明县| 定南县| 南郑县| 城固县| 蚌埠市| 永顺县|