7

I've got the following HTML:

<i style="cursor:pointer" ng-click="addName()" class="icon-plus"></i>
<i style="cursor:pointer" ng-click="delName({{$index}})" class="icon-remove"></i>

And the following functions in my controller's $scope:

$scope.addName = function() {
    $scope.names.push($scope.newName);
    $scope.newName = '';
};
$scope.delName = function(i) {
    $scope.names.splice(i, 1);
};

The addName() works fine but the delName() is never called. Is it impossible to bind an ng-clik to a function with an argument?

3 Answers 3

14

The error was in the html, the ng-repeat $index should not be evaluated beforehand:

This is the valid HTML:

<i style="cursor:pointer" ng-click="delName($index)" class="icon-remove"></i>
Sign up to request clarification or add additional context in comments.

6 Comments

I am having similar problem, updateProductState({{product_id}}), it doesn't trigger the function, but if I hard code a static value it would work. any idea? thanks
have you tried updateProductState(product_id)? You don't need the {{ }} in an ng directive
Thanks. The above helped my problem. Can you explain why {{ }} is not needed in a directive?
@TyMayn In fact it's the other way around, it's because you are not in a directive that you need the {{ }}. Most angular directives are evaluating expressions (see docs.angularjs.org/api/ng.directive:ngClick and docs.angularjs.org/guide/expression). The {{ }} are used to evaluate expressions outside of a directive in the HTML at compile time (see docs.angularjs.org/api/ng.$interpolate).
@plus- Thanks. That helps a lot. God bless stackoverflow :)
|
0

The code seems fine to me, can you isolate your problem in a jsFiddle?

EDIT: Removed incorrect answer about splice not modifying to array.

2 Comments

Plain wrong: splicedoes modify the original array and returns the REMOVED items. My problem is the function is never called anyway.
Ah, you're right, I'm tired I guess. Just answered another question about Array.filter which returns a new array. Sorry.
0

You can do as follows

<i ng-click="delName($index)" class="icon-remove"></i>

In css

[ng-click],
[data-ng-click],
[x-ng-click] {
    cursor: pointer;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.