AngularJS - Url format

advertisements

I'm having trouble with AngularJs's url

I need something like this:

/api/menu/3

But Im getting this:

/api/menu?menuId=3

I need this format beacuse I'm using Laravel.

app.js:

var app = angular.module('mainModule', ["ngRoute", "ngResource", 'mainModule.services', 'mainModule.controllers'])
        .config(function($routeProvider, $locationProvider) {
            $routeProvider
            .when("/", {
                templateUrl : "templates/dashboard.html",
                controller  : "contentController"
            })
            .when("/:id", {
                templateUrl : "templates/dashboard.html",
                controller  : "contentController"
            })
            .when("/config/:id", {
                templateUrl : "templates/config.html",
                controller  : "configmenuController"
            })
            .when("/account", {
                templateUrl : "templates/account.html"
            })
            .otherwise({
                templateUrl : "templates/error.html"
            })
            //$locationProvider.html5Mode(true);
        }
);`

controller.js

angular.module('mainModule.controllers',[]);

angular.module('mainModule.controllers')
.controller('configmenuController', function($scope, $http, $routeParams, API_URL, Submenu) {
    var id=$routeParams.id
    $scope.submenu = Submenu.query({ menuId : id }, function() {
        console.log($scope.submenu);
    });
});

services.js

var API_URL = 'http://test.com'

angular.module('mainModule.services', [])
    .constant('API_URL', 'http://test.com')
    .factory('Submenu', function($resource) {
       return $resource( API_URL + '/api/menu', { menuId : '@menuId'}, {
            update: {
              method: 'PUT'
            }
        });
    });

I think the problem is in services.js, but I can't find a way to do it.


Replace:

$resource( API_URL + '/api/menu', { menuId : '@menuId'}, { ...

with:

$resource( API_URL + '/api/menu/:menuId', { menuId : '@menuId'}, { ...

Yes, just add /:menuId to your URL. $resource replaces :attr for the value from attr property your the object. If :attr is not found, it's simply added to the URL as a query param. That's what was happening.