Skip to content

Instantly share code, notes, and snippets.

@rit

rit/-

Created June 6, 2014 19:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rit/b45bbc405dfac92afa42 to your computer and use it in GitHub Desktop.
Save rit/b45bbc405dfac92afa42 to your computer and use it in GitHub Desktop.
diff --git a/app/scripts/directives/mycollection.js b/app/scripts/directives/mycollection.js
index 0f850bf..529951e 100644
--- a/app/scripts/directives/mycollection.js
+++ b/app/scripts/directives/mycollection.js
@@ -4,10 +4,10 @@ angular.module('testingApp')
.directive('myCollection', function ($http) {
return {
template:
- '<div>' +
+ '<div ng-click="remove($event)">' +
'<div ng-repeat="item in list">' +
'<input type="text" ng-model="item.name" />' +
- '<a class="remove" ng-click="remove($index)">Remove</a>' +
+ '<a class="remove" data-index={{$index}}>Remove</a>' +
'</div>' +
'<a ng-click="add()">Add</a>' +
'</div>',
@@ -19,8 +19,14 @@ angular.module('testingApp')
scope.list.push({name: ""});
}
- scope.remove = function (index) {
+ scope.remove = function (event) {
+ // We only catch click event emitted from "Remove" button
+ var idx = angular.element(event.target).data("index");
+ if (idx == null) return;
+
+ var index = parseInt(idx);
scope.list.splice(index, 1);
+ event.preventDefault();
}
if (scope.url) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment