javascript - 单击带有参数的链接后,Angularjs 更新了表格

标签 javascript angularjs single-page-application ngroute

我正在制作一个单页应用程序

我的配置:

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/

相关文章:

javascript - 扩展 Array.prototype 崩溃

javascript - 在此模块外的 Node.js 模块中模拟构造函数(或其他函数)

javascript - 无法让动态模板在 Angular 指令中工作

playframework-2.0 - Play 2 中通配符路由到静态文件

javascript - requirejs如何加载commonjs包?

javascript - Electron 生成器: “Error occurred in the main process - Callback is not a function”

javascript - 无法将响应值从nodejs服务器传递到ajax函数

javascript - Ionic2 禁用标签元素上的 300 毫秒延迟

javascript - Angularjs 如何显示列表框中所选行的隐藏数据?

javascript - 如何从 url 中提取 id?