Serge S
Serge S

Reputation: 3

How can I pass an item id to my controller for deletion method?

I have a view with set of items and delete button for each item

...<tr ng-repeat="course in vm.courses">
        <td>{{ course.name }}</td>
        <td>{{ course.url }}"</td>
        <td>{{ course.duration }}</td>
        <td>
<a ng-click="deleteCourse(course.id)" class="btn btn-sm btn-danger">Delete course</a>...

And I need to pass course.id to angularjs controller for deletion

(function () {

"use strict";

angular.module("app-courses")
.controller("coursesController", coursesController);

function coursesController($routeParams, $http){

    var vm = this;

    vm.deleteCourse = function (id) {

        $http.delete("/api/courses/" + id)
                .then(function (response) {
                    vm.courses.splice(id, 1);
                }
    };
}})();

And then url and id go to API Controller.

But this way it doesn't work. Probably I'm doing something wrong both in view and controller. How can I set this up in angular?

Upvotes: 0

Views: 82

Answers (1)

GregL
GregL

Reputation: 38121

You most likely forgot the vm. before the deleteCourse(course.id) in your ng-click expression.

It's usually the little things.

P.S. You should always use track by for ng-repeat expressions. In your case, use the expression "course in vm.courses track by course.id".

Upvotes: 1

Related Questions