Skip to content

Instantly share code, notes, and snippets.

@allenyang79
Created August 29, 2013 05:38
Show Gist options
  • Save allenyang79/6374580 to your computer and use it in GitHub Desktop.
Save allenyang79/6374580 to your computer and use it in GitHub Desktop.
angularjs ng-repeat $on $emit
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body ng-app>
<div ng-controller="Ctrl">
<ul class="" >
<li ng-repeat="item in items">
<span>{{item.title}}</span>
<input ng-model="item.quantity" ng-change="$emit('change');"/>
<span>{{item.price | currency}}</span>
<span>{{item.price * item.quantity | currency}}</span>
<button ng-click="remove($index)">Remove</button>
</li>
</ul>
total:{{total}}
</div>
</body>
</html>
function Ctrl($scope){
//$scope.total=100;
$scope.items=[];
$scope.items.push({
title:"A",
price:10,
quantity:10
});
$scope.items.push({
title:"B",
price:120,
quantity:2
});
$scope.items.push({
title:"C",
price:50,
quantity:3
});
$scope.$on('change',function(e){
console.log('change');
console.dir(arguments);
e.currentScope.countTotal();
});
$scope.countTotal=function(){
var v=0;
for(var i=0;i<this.items.length;i++){
var item=this.items[i];
v+=item.price*item.quantity;
}
$scope.total=v;
};
$scope.remove = function(index){
$scope.items.splice(index, 1);
};
$scope.countTotal();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment