2016-04-20 447 views
1

我有一個ng-click按鈕,我通過paramater將一個對象傳遞給一個函數,該函數必須將我重定向到另一個頁面,其中包含傳遞給它的對象的id,另一個控制器接收它,在bd中搜索並在我的視圖中顯示它,但按鈕不工作,它只是將我重定向到$ routeProvicer.otherwise('/')家中。但如果我輸入manualy localhost:3000 /管理/ 1241245124 < =它的工作原理完美...重定向不起作用

任何幫助是apreciated,我已經離開你在這裏我的代碼。

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; 
    }) 
}); 
+0

嘗試沒有''#。即:'$ location.url('/ admin /'+ book._id);'。 – Lex

+0

它不工作...:/ – FridoxFL

回答

1

嘗試從<a> href屬性刪除#讓它成爲href=""

+0

是的,那是錯誤,謝謝你:) – FridoxFL