0

I am learning angularjs and I am able to list out my data I receive via a REST API. I display the data in a table and in one of the columns I want to have a delete button that will eventually make a DELETE call to remove that particular record from the database. Here is the HTML I using:

<table st-table="displayedCollection" st-safe-src="hosts" class="table table-striped">
    <thead>
    ...
    </thead>
    <tbody> 
        <tr ng-repeat="x in displayedCollection">
            <td>{{x.hostname}}</td>
            <td>{{x.ipaddress}}</td>
            <td>{{x.macaddress}}</td>            
            <td>
                <button 
                   type="button" 
                   class="btn btn-default btn-sm" 
                   ng-click="delete_record({{x._id}})">
                   <span class="glyphicon glyphicon-remove"></span>
                </button>
            </td>
        </tr>
    </tbody>
    ...    
   </table>

This gives me this error:

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 16 of the expression [delete_record({{x._id}})] starting at [{x._id}})].

My controller looks like this:

app.controller("meanVoyCtrl", function($scope,$http) {
    ...        
    $scope.delete_record  = function(id) {alert("id is ["+id+"]");};
    ...
});

For now I'd be happy if I could just an alert pop up display the _id of the record to be deleted.

What am I doing wrong here?

Thanks

3 Answers 3

1

Get rid of the curlys, just do:

ng-click="delete_record(x._id)">
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need the {{}} around x._id. Should look like this:

ng-click="delete_record(x._id)"

Comments

1

You only need to use curly braces if you are outside an angular directive ('ng-repeat' in this case). It serves as a way to use your angular data or controller code. Because you are inside an angular directive already, the curly braces are not needed. Your button html would become:

ng-click="delete_record(x._id)">

2 Comments

"You only need to use curly braces if you are outside an angular directive" - Whatever you mean by outside, the statement is simply not true. Whether or not curly braces can be used depends on what a directive expects. A bunch of them do expect templates, ngHref e.g. The reason the OP's code doesn't work is that the expression is parsed before it has been interpolated. Otherwise it would work even with the curly braces.
Yes, you are correct. I didn't feel going into that much detail on this simple of a problem was necessary since he is not using an angular directive that does expect a template.

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.