Sami
Sami

Reputation: 2331

Wrong route in AngularJS app?

I am using the same html-page for creating an new rma and editing the old one. I am trying to implement routing in AngularJs but it is not working when choosing to edit old object from a list of rma list.

Routing

.config(function ($routeProvider) {
$routeProvider
  .when('/', {
    templateUrl: 'views/main.html',
    controller: 'MainCtrl'
  })
  .when('/rma', {
    templateUrl: 'views/rma.html',
    controller: 'RmaCtrl'
  })
  .when('/rma/:rmaId', {
    templateUrl: 'views/rma.html',
    controller: 'RmaCtrl'
  })
  .when('/rma-list', {
    templateUrl: 'views/rma-list.html',
    controller: 'RmaListCtrl'
  })

RMA list controller

        $scope.updateRma = function (rmaId) {
            console.log("updateRma---->" +rmaId);
            $location.path('/rma/'+rmaId);
            console.log("2. ABSURL---->" +$location.absUrl());
            // ABSURL---->http://localhost:8383/RmaClient/app/index.html#/rma-detail/%5Bobject%20Object%5D

        };

rma-list.html

<td><a ng-click="updateRma(rma.rmaId)" class="btn btn-small btn-success">edit</a></td>

rma controller rma.js

angular.module('rmaClientApp')
    .controller('RmaCtrl',
            function ($scope, $location, rmaService, $routeParams) {
                console.log("------RmaCtrl------" +$routeParams.rmaId);
                //When creating a new one, should be 0
                $scope.editMode = ($routeParams.rmaId === "0");
                if ($scope.editMode) {
                    console.log("----Creating a new RMA-----");
                    $scope.rma = rmaService.get({id: $routeParams.rmaId});
                    //console.log("$scope.rma --->" +$scope.rma);
                    $scope.rmaService = rmaService;
                } else {
                    console.log("----Editing an old RMA rmaId: "+$routeParams.rmaId );
                    var rma = $scope.rma;
                    $scope.rma = rmaService.get({id: $routeParams.rmaId});

                }

rmaService

angular.module('rmaServices', ['ngResource'])
    .factory('rmaService', ['$resource',
        function ($resource) {
            return $resource(

                    'http://localhost:8080/RmaServer/webresources/com.sako.rma.rma/:rma:rmaId',
                    {},
                    {

                        update: { method: 'PUT', params: {id: 'rmaId'} }
                    });
        }]);

Problem

In server side the REST-service is working fine, but when trying to edit rma number 7 in client side, I just got a blank page and error message. This link returns the corrent rma whit ID=7 and the link is:

http://localhost:8080/RmaServer/webresources/com.ako.rma.rma/7

When I am trying to choose the same rma from the web page the link is following:

http://localhost:8383/RmaClient/app/index.html#/rma/7

Error

------RmaCtrl------7 (12:28:34:557)

at app/scripts/controllers/rma.js:13 Error: [$resource:badcfg] Error in resource configuration for action get. Expected response to contain an object but got an array http://errors.angularjs.org/1.3.15/$resource/badcfg?p0=get&p1=object&p2=array

Errorline is this: $scope.rma = rmaService.get({id: $routeParams.rmaId});

Upvotes: 0

Views: 523

Answers (1)

sylwester
sylwester

Reputation: 16498

Your resource url should be

http://localhost:8080/RmaServer/webresources/com.sako.rma.rma/:id'

then

http://localhost:8080/RmaServer/webresources/com.sako.rma.rma/:rma:rmaId'

as you passing only one parameter rmaId

   angular.module('rmaServices', ['ngResource'])
        .factory('rmaService', ['$resource',
            function ($resource) {
                return $resource(

                        'http://localhost:8080/RmaServer/webresources/com.sako.rma.rma/:id',
                        {id:"@id"},
                        {

                            update: { method: 'PUT', params: {id: 'rmaId'} }
                        });
            }]);

Upvotes: 1

Related Questions