1.angular.copy
angular.copy(source, [destination]);- If no destination is supplied, a copy of the object or array is created.如果目标对象没有提供,就会创建对象或者是数组的复制份
- If a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted and then all elements/properties from the source are copied to it.
- If source is not an object or array (inc. null and undefined), source is returned.
- If source is identical to 'destination' an exception will be thrown.
angular-copy demo jsvar APP = angular.module('app', []) APP.controller('ctrl', ['$scope', function($scope) { //angular.copy(source, [destination]); //1,If no destination is supplied, a copy of the object or array is created.如果目标对象没有提供,就会创建对象或者是数组的复制份 //2,If a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted and then all elements/properties from the source are copied to it. //3,If source is not an object or array (inc. null and undefined), source is returned. //4,If source is identical to 'destination' an exception will be thrown. $scope.master= {}; $scope.update = function(user) { // Example with 1 argument $scope.master= angular.copy(user);//没有提供destination,就会创建,并且赋值给master }; $scope.reset = function() { // Example with 2 arguments angular.copy($scope.master, $scope.user);//destination提供了,删除原有的元素,然后从原数据源中copy所有的数据 }; $scope.reset(); }]);form = { {user | json}}master = { {master | json}}
页面显示效果:
2,$watch函数 使用$watch函数监控数据模型的变化,当你的数据模型的某一部分发生变化的时候,$watch函数可以向你发出通知,你可以监控每一个对象的属性,也可以监控需要经过计算的结果(函数),实际上只要能够被当作属性访问到,或者可以被当作js函数计算出来,就可以被$watch函数监控: $watch(watchFn,watchAction,deepWatch) watchFn:该参数是一个带有angular表达式或者函数的字符串,它会返回被监控数据模型的当前值。 watchAction:这是一个函数或者表达式,当watchFn发生变化时会被调用。 deepWatch:如果设置为true,这个可选的布尔型参数将会命令angular去检查被监控对象的每一个属性是否发生变化。 $watch函数会返回一个函数,当你不再需要接受变更通知时,可以利用这个返回的函数注销监听器. 如果我们需要监听一个属性,然后接着注销监控,我们可以使用如下代码: var item = $scope.$watch('module.property',callbackonchange());购物车(2) { {item.title}} { {item.price | currency}} { {item.price * item.quantity | currency}}总消费: { {totalCart() | currency}}打折: { {bill.discount | currency}}打折后消费: { {subtotal() | currency}}