frankclaassens
frankclaassens

Reputation: 1930

How to call GET WEB API method from AngularJS

I'm using AngularJS with MVC4. When I click on a row inside data-ng-grid, I want it to call the GET method from my Web API. BUT... I want it to call the GET BY ID method. For some reason it always calls the GET method that retrieves all the Movies. How do I call the GET method that uses the param id? not sure what im doing wrong.

var movieResource = $resource('/api/movie', {}, { update: { method: 'PUT' }, getById: { method: 'GET', params: 'id' } });

    $scope.$watchCollection('selectedMovies', function () {
        //$scope.selectedMovie = angular.copy($scope.selectedMovies[0]);
        movieResource.getById(5);
    });


    public MovieData Get(int id)
    {
        var movie = movieRepository.GetDataById(id);
        if (movie == null)
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }

        return movie;
    }

MovieController

    // GET api/movie
    public IEnumerable<MovieData> Get()
    {
        return movieRepository.GetAllData().AsEnumerable();
    }

    // GET api/movie/5
    public MovieData Get(int id)
    {
        var movie = movieRepository.GetDataById(id);
        if (movie == null)
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }

        return movie;
    }

Upvotes: 0

Views: 1331

Answers (1)

Aray Karjauv
Aray Karjauv

Reputation: 2945

According to $resource documentation you have to provide params like this: movieResource.get({id: 5})

Also you can use $http service $http.get('/api/movie/'+id)

Upvotes: 1

Related Questions