国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Angular中的$watch、$watchGroup、$watchCollection

2019-11-19 16:15:43
字體:
來源:轉載
供稿:網友

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

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

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

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

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

• 4,參數:objectEquality(非必須):是否監視個對象,默認為false

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

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

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

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

• 6,$watch一個對象。

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

•    如果監測對象中某一屬性,可以寫user.name的形式,第三個參數默認;

•     如果監測對象中全部屬性,$watch對象名,第三個參數true;

• 7,$watchGroup,第一個參數是一個表達式的數組或者返回表達式的數組的函數。

• 8,$watchCollection;

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

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

•9,注銷$watch

•         $watch函數返回一個注銷監聽的函數,太多的$watch將會導致性能問題,$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') {//設置一個注銷監聽的條件              watcher(); //注銷監聽            }          });          // $watchGroup          $scope.$watchGroup(["value2", "value3"], function (newVal, oldVal) {            //注意:newVal與oldVal都返回的是一個數組            $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();//          這里要監聽數量變化計算綜合          //一 只監聽所有num變化計算總額          var watchArr = [];          $scope.items.goodsArr.forEach(function (v, i) {            watchArr.push("items.goodsArr[" + i + "]['num']");          });          $scope.$watchGroup(watchArr, function (newVal, oldVal) { //注意:newVal與oldVal都返回的是一個數組            $scope.items.sum = 0;            $scope.items.goodsArr.forEach(function (v, i) {              $scope.items.sum += v.price * (v.num > 0 ? v.num : 0);            });          });/*          //二 這樣寫則監聽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,參數:watchExp(必須):{(function()|string)},可以字符串表達式,也可以帶當前scope為參數的函數     *    - `string`: Evaluated as {@link guide/expression expression}     *    - `function(scope)`: called with current `scope` as a parameter.     * 3,參數:listener(必須):function(newVal, oldVal, scope),觀察的表達式變化的時候調用的函數。     * 4,參數:objectEquality(非必須):是否監視個對象,默認為false     * 5,$scope.$digest().會執行所有的同$scope下的$watch。     *  但會出錯$apply already in progress,換了$rootScope也一樣。     *  原因-參考大牛博客:http://blog.csdn.net/aitangyong/article/details/48972643     *  $digest、$apply、$$phase這些屬性或者方法其實都是$scope中的私有的,最好不要使用。     * 6,$watch一個對象。     *  如果要監視對象的變化(地址改變),$watch對象名,第三個參數默認;     *  如果監測對象中某一屬性,可以寫user.name的形式,第三個參數默認;     *  如果監測對象中全部屬性,$watch對象名,第三個參數true;     * 7,$watchGroup,第一個參數是一個表達式的數組或者返回表達式的數組的函數。     * 8,$watchCollection;     *   js中數組也是對象,但按照$watch一個對象的方式,只有數組引用變了才能監聽變化,增加刪除$watch監聽不到,所以就有了$watchCollection。     *   function(obj, listener):第一個參數必須對象或者返回對象的函數。     */    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將會導致性能問題,$watch如果不再使用,我們最好將其釋放掉。             *$watch函數返回一個注銷監聽的函數,如果我們想監控一個屬性,然后在稍后注銷它,可以使用下面的方式:             */            if (newVal == 'clear') {//設置一個注銷監聽的條件              watcher(); //注銷監聽            }          });          // $watchGroup          $scope.$watchGroup(["value2", "value3"], function (newVal, oldVal) {            //注意:newVal與oldVal都返回的是一個數組            $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();//          這里要監聽數量變化計算綜合          //一 只監聽所有num變化計算總額          var watchArr = [];          $scope.items.goodsArr.forEach(function (v, i) {            watchArr.push("items.goodsArr[" + i + "]['num']");          });          $scope.$watchGroup(watchArr, function (newVal, oldVal) { //注意:newVal與oldVal都返回的是一個數組            $scope.items.sum = 0;            $scope.items.goodsArr.forEach(function (v, i) {              $scope.items.sum += v.price * (v.num > 0 ? v.num : 0);            });          });/*          //二 這樣寫則監聽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,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大方县| 驻马店市| 邹平县| 库尔勒市| 台前县| 清远市| 铜梁县| 贵阳市| 曲松县| 黄浦区| 黎平县| 察哈| 手游| 沛县| 始兴县| 宜君县| 台中县| 彰化市| 灵宝市| 庆安县| 濮阳市| 池州市| 读书| 张家川| 大渡口区| 江西省| 南投县| 玉田县| 新丰县| 马边| 托克托县| 察雅县| 龙州县| 思茅市| 八宿县| 岑溪市| 怀柔区| 息烽县| 宝鸡市| 林周县| 遂昌县|