FridoxFL FridoxFL - 7 months ago 18
Javascript Question

Redirection not working

i have a button with ng-click which i pass an object by paramater to a function, that function have to redirect me to another page with the id of the object i've passed to it, and another controller receive it, search it in the bd and show it in my view, but the button it's not working, it just redirrects me to the $routeProvicer.otherwise('/') the home. But if I type manualy localhost:3000/admin/1241245124 <= the id it works perfect...

Any help is apreciated, i've leave you down here my code.

admin.html

<h1>Admin Panel</h1>
<hr>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Productos</h3>
</div>
<div class="panel-body">
<table class="table">
<tr>
<td>Titulo</td>
<td>Tipo</td>
<td>Autor</td>
<td>Creado</td>
<td>Acciones</td>
</tr>
<tr ng-repeat="product in products">
<td><a ng-href="{{}}">{{product.nombre}}</a></td>
<td>{{product.tipo}}</td>
<td>{{product.autor}}</td>
<td>{{product.creado | date}}</td>
<td><a href="#" class="btn btn-warning" ng-click="deleteBook(product)">Borrar</a> &nbsp; &nbsp;<a href="#" class="btn btn-info" ng-click="editBook(product)">Editar</a></td>

</tr>
</table>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Articulos</h3>
</div>
<div class="panel-body">
Panel content
</div>
</div>
</div>
</div>


admin.js

'use strict';

angular.module('myApp.admin', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/admin', {
templateUrl: 'admin/admin.html',
controller: 'AdminCtrl'
});
$routeProvider.when('#/admin/:id', {
templateUrl: 'admin/edit.html',
controller: 'AdminEditCtrl'
});
}])

.controller('AdminCtrl', function($scope, $http,librosFactory, $location) {

librosFactory.getLibros().success(function(libros){
$scope.products = libros;
});

$scope.deleteBook = function(book){
console.log(book);
librosFactory.deleteBook(book).success(function (book,err) {
if (err) console.log(err);
else console.log("libro borrado");
});
};

$scope.editBook = function (book) {
if(book._id) {
console.log(book);
$location.url('#/admin/'+book._id);
}
else {
console.log('erroror');
}

}


})
.controller('AdminEditCtrl', function ($scope, $routeParams, librosFactory) {
librosFactory.searchById($routeParams.id).success(function (book, err) {
if(err) console.log(err);
console.log(book)

$scope.product = book;
})
});

Answer

Try removing the # from the href attribute in the <a> let it be href=""