I am trying to call a function inside a directive. Here is a html:
<span class="delete-link">
<delete></delete>
<input type="button" data-ng-click="removeRow(task)"/>
</span>
And here is the directive:
.directive('delete', function() {
return {
restrict: 'E',
replace: true,
template: '<div></div>',
link: function(scope, element) {
element.click(function(){
$scope.removeRow = function (task) {
$scope.tasks.splice($scope.tasks.indexOf(task), 1);
}
});
}
}
});
This is the example I used:
http://jsfiddle.net/mrajcok/T96Zu/
But it doesn't delete an element. What I'm missing?
.directive('delete', function() {
return {
restrict: 'E',
replace: true,
template: '<div></div>',
link: function(scope ,element) {
scope.removeRow = function (task) {
scope.tasks.splice(scope.tasks.indexOf(task), 1);
}
}
}
});
Use scope instead of $scope
.directive('delete', function() {
return {
restrict: 'E',
replace: true,
template: '<div></div>',
link: function(scope, element) {
element.click(function(){
scope.removeRow = function (task) {
scope.tasks.splice(scope.tasks.indexOf(task), 1);
}
});
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.