我正在制作一个单页应用程序
我的配置:
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 中的记录,但它不会像我期望的配置中的 templateUrl 那样“刷新”我的页面。
用户必须重新加载页面,表格才能显示正确的数据。有什么想法可以指导我找到解决方案吗?
最佳答案
您想要删除 Wine 并将用户重定向到另一个页面还是只想删除 Wine ?
.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.
}
};
可以在此处找到如何在 Controller 之间共享数据的示例: Share data between AngularJS controllers
关于javascript - 单击带有参数的链接后,Angularjs 更新了表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42320855/