2017-02-18 49 views
0

我想提出一個單頁應用程序的鏈接後Angularjs更新一下表格與參數

我的配置:

var app = angular.module('menuCardMaker', ['ngRoute']); 

app.config(function ($routeProvider, $locationProvider) { 
    $routeProvider 
    .when('/wines', { 
     templateUrl: 'templates/wine/wine.html', 
     controller: 'WineController' 
    }) 
    .when('/wines/delete/:id', { 
     templateUrl: 'templates/wine/wine.html', 
     controller: 'WineController' 
    }) 

我的HTML:

 <table> 
     <tr> 
      <td class="head">Name</td> 
     </tr> 
     <tr ng-repeat="wine in winesFromStorage"> 
      <td>{{ wine.name }}</td> 
      <td><a ng-href="#/wines/delete/{{ wine.id }}" ng-click="remove()">Delete</a></td> 
     </tr> 
    </table> 

的URL頁面負載(例如)http://127.0.0.1:8080/#/wines/delete/1當用戶點擊刪除。它會刪除LocalStorage中的記錄,但它不會像我期望從我的配置中的templateUrl那樣「刷新」我的頁面。

用戶必須在表格顯示正確的數據之前重新加載頁面。任何想法可以指導我解決問題?

回答

1

是否要刪除葡萄酒並將用戶重定向到另一個頁面,或者您只是想刪除葡萄酒?

.controller('WineController', ['$scope', '$location' function ($scope, location) { 
    $scope.wines = ['wine1', 'wine2]; 

    $scope.deleteWine = function(wineIndex){ 
     // this should update the ng repeat list on your page 
     delete $scope.wines[wineIndex]; 
     // if you still want to redirect the user you could do 
     // it like this: 
     $location.path('/yoururlhere'); 
     // of course this would load another route with another controller. 
     //If you want to share the current wine list between two 
     //controllers, you could create a wineListService where 
     //you store the list. 
    } 

}; 

一個例子如何共享控制器之間的數據可以在這裏找到: Share data between AngularJS controllers

1

從存儲刪除記錄後,您可以指定對象在使用的數組的更新陣列NG-重複

$scope.remove = function(){ 

    $scope.winesFromStorage = updatedLocalStorageObject; 

} 

這樣,你不會有重新加載頁面,因爲它的雙向綁定它會自動重新加載數據部分。

+0

這應該工作。但是,我的頁面加載/葡萄酒/與表,當按下刪除它指向/葡萄酒/刪除/ 1。所以它看起來像它保持/葡萄酒/並且它不更新數據。有任何想法嗎? 我喜歡這個想法比上面的更好! – Bilzard