-
-
Save rit/b45bbc405dfac92afa42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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