我正在制作单页申请
我的配置:
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>
当用户点击“删除”时,页面会在http://127.0.0.1:8080/#/wines/delete/1上加载URL(例如)。它会删除我的LocalStorage中的记录,但它不会刷新&#39;我的页面就像我期望从我的配置中的templateUrl。
用户必须在表格显示正确的数据之前重新加载页面。有什么想法可以指导我找到解决方案吗?
答案 0 :(得分:1)
从存储中删除记录后,您可以将更新的对象数组分配给ng-repeat中使用的数组
$scope.remove = function(){
$scope.winesFromStorage = updatedLocalStorageObject;
}
这样你就不必重新加载页面,因为它的双向绑定将自动重新加载数据部分。
答案 1 :(得分: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