Angular中的$watch、$watchGroup、$watchCollection
(编辑:jimmy 日期: 2024/11/16 浏览:3 次 )
"external nofollow" href="mailto:{@link">{@link guide/expression expression}
"external nofollow" href="http://blog.csdn.net/aitangyong/article/details/48972643">http://blog.csdn.net/aitangyong/article/details/48972643
"color: #ff0000">一、使用方法
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 "htmlcode"><!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="/UploadFiles/2021-04-02/angular.min.js">以上所述是小编给大家介绍的Angular中的$watch、$watchGroup、$watchCollection,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
下一篇:JS实现加载时锁定HTML页面元素的方法
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。