0

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?

Serge S
  • 3
  • 1
  • 1
    `it doesn't work` isn't a proper problem description. What specifically doesn't work? Note that `splice()` needs the index...not the id see http://stackoverflow.com/questions/15453979/how-do-i-delete-an-item-or-object-from-an-array-using-ng-click/15454424#15454424 – charlietfl Jan 11 '16 at 23:53
  • What does't work? you call to the API/MVC controller or the angular part? I'm confused now. – jpgrassi Jan 12 '16 at 00:07
  • @jpgrassi if you don't understand why did you answer? – charlietfl Jan 12 '16 at 00:15
  • well when I first read his question, I thought the problem was he couldn't send the parameter to the Delete method. But after reading it again (and after posting my answer) I wasn't so sure. But, since he didn't post his controller, I thought leaving my answer because he might have this problem after the angular part is fixed. – jpgrassi Jan 12 '16 at 00:20
  • @charlietfl I deleted my answer since it was not primarily related to the problem. Thanks for pointing that out ;) – jpgrassi Jan 12 '16 at 00:53

1 Answers1

1

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".

GregL
  • 37,147
  • 8
  • 62
  • 67