angular-fullstack / generator-angular-fullstack

Yeoman generator for an Angular app with an Express server
https://awk34.gitbook.io/generator-angular-fullstack
6.12k stars 1.24k forks source link

API url redirect to homepage after deletion #535

Closed flt123 closed 10 years ago

flt123 commented 10 years ago

I'm experiencing a situation that I don't know if it is a bug or rookie issue.

I generated a new route and and new endpoint, let's call it message. I made it exact replica of api\thing and just changed thing to message in the route and the endpoint.

I noticed that when I delete anything from the new route: localhost:8000\message, I get redirected to localhost:8000. This does not happen when I add anything. Is this a bug or am I missing something?

kingcody commented 10 years ago

Would help if we had some code to examine :smile:

flt123 commented 10 years ago

Thanks kingcody.

For client, I have recipient.js:

'use strict';

angular.module('mastersocketApp') .config(function ($stateProvider) { $stateProvider .state('recipient', { url: '/recipient', templateUrl: 'app/recipient/recipient.html', controller: 'RecipientCtrl', authenticate: true,

  });

});

I have recipient.controller.js: 'use strict';

angular.module('mastersocketApp') .controller('RecipientCtrl', function ($scope, $http, socket) { $scope.awesomeRecipients = [];

$http.get('/api/recipients').success(function(awesomeRecipients) {
  $scope.awesomeRecipients = awesomeRecipients;
  socket.syncUpdates('recipient', $scope.awesomeRecipients);
});

$scope.addRecipient = function() {
  if($scope.newRecipient === '') {
    return;
  }
  $http.post('/api/recipients', { name: $scope.newRecipient });
  $scope.newRecipient = '';
};

$scope.deleteRecipient = function(recipient) {
  $http.delete('/api/recipients/' + recipient._id);
};

$scope.$on('$destroy', function () {
  socket.unsyncUpdates('recipient');
});

});

On the server/api/recipient, I have index.js: 'use strict';

var express = require('express'); var controller = require('./recipient.controller');

var router = express.Router();

router.get('/', controller.index); router.get('/:id', controller.show); router.post('/', controller.create); router.put('/:id', controller.update); router.patch('/:id', controller.update); router.delete('/:id', controller.destroy);

module.exports = router;

I used angular-fullstack:route and angular-fullstack:endpoint to generate both of them. They are exactly same as app/main on the client side and api/thing on the server side. The only thing I changed is 'thing' to 'recipient'.

Hope that provides some more clarity. Thanks once again.

flt123 commented 10 years ago

Sorry I mistakenly closed the issue

kingcody commented 10 years ago

What does client/app/recipient/recipient.htm look like?

kingcody commented 10 years ago

This line:

<li><a href="#" tooltip="{{recipient.info}}">{{recipient.name}}<button type="button" class="close" ng-click="deleteRecipient(recipient)">&times;</button></a></li>

remove # from href="#"

flt123 commented 10 years ago

Thanks a lot kingcody, that fixed it. wondering why though?

kingcody commented 10 years ago

Glad I could help :)

To answer your question, it's because of angular's routing, in particular html5Mode. When you clicked on the button in the anchor tag, the event bubbles up and the anchor tag's href is fired, which would add a "hashbang" to the URL. Since html5Mode is enabled and angular did not find a route that matched you were being redirected to /.

flt123 commented 10 years ago

Thanks a lot kingcody for the explanation