cpursley
cpursley

Reputation: 182

Angular $resource: How can I transfer to 'edit' view after 'new' save action?

I've working on an Angular app with CRUD actions to read/write to/from an API. How can I transfer to the edit view after save action? The following results in a url of /users/undefined/edit instead of injecting userId.

New controller

.controller('UserNewCtrl', [
  '$scope'
  '$location'
  'Users'

  ($scope, $location, Users) ->
    $scope.saveUser = (userId) ->
      Users.save($scope.user) 
      # how can I get to /edit view after save action?
      $location.path('users' + userId + '/edit') 
      return
])

Factory

.factory('Users', [
  '$resource'

  ($resource) ->
    $resource('http://myapi/users', {})
])

.factory('User', [
  '$resource'

  ($resource) ->
    $resource(http://myapi/users/:id', {},
      update:
        method: 'PUT'
        params:
          id: '@id'
    )
])

Upvotes: 0

Views: 87

Answers (1)

Callebe
Callebe

Reputation: 1097

The save operation of $resource receive 2 parameters, a success callback and a error callback. The success callback receive as first parameter the response object, that is the user saved. This object should get an Id.

I thing that is better define the $scope.user as an User instance like this:

$scope.user = new User();

Then, save the user object.

$scope.user.save(function (u) {
    // redirect url using u.id or $scope.user.id;
});

So... you have to wait for callback.

For more information about $resource, please, read the doc

Upvotes: 2

Related Questions