3

I am building an application using ASP.NET and AngularJs. I am trying to pass object id from the view (HTML) to the Angular controller, to then make an http call to the api. I don't know how to pass the id of an specific object from this table when the delete link is clicked.

<table class="table table-responsive table-striped">
        <tr ng-repeat="post in viewModel.posts">

            <td>{{ post.text }}</td>
            <td>{{ post.postedOn | date:'medium' }}</td>
            <td><a href="#" class="btn btn-sm">Edit</a></td>

            <td><a ng-click="viewModel.deletePost" class="btn btn-sm">Delete</a></td> 

        </tr>
</table>

and this is how I am trying to make the call to the API from the controller

// delete post function
viewModel.deletePost = function () {

        viewModel.errorMessage = "";

        $http.delete("http://localhost:61878/api/posts", viewModel.newPost.id)
            .then(function (response) {

                // sucess delete
                viewModel.posts.delete(response.data);

                viewModel.newPost = {};

            }, function () {
                // failed to delete

                viewModel.errorMessage = "Failed to delete post"
            })
            .finally(function () {

            });
    };

Does anybody know how can I pass the id of the respective post on the row clicked to the Angular controller?

1 Answer 1

4

viewModel.deletePost is a function - just call it with parameters.

<a ng-click="viewModel.deletePost(post.id)" class="btn btn-sm">Delete</a>

After that you just have to add the parameter to the function:

viewModel.deletePost = function (id) { ... }
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome it worked! The http call is failing but that is something I can figure out. Thank you!

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.