【问题标题】:Angularjs Updated table after clicking a link with parameters单击带有参数的链接后,Angularjs更新了表格
【发布时间】:2017-07-08 07:59:28
【问题描述】:

我正在制作单页应用程序

我的配置:

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 中“刷新”我的页面。

用户必须在表格显示正确数据之前重新加载页面。有什么想法可以指导我找到解决方案吗?

【问题讨论】:

    标签: javascript angularjs single-page-application ngroute


    【解决方案1】:

    您是要删除 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. 
         }
    
    };
    

    可以在此处找到如何在控制器之间共享数据的示例: Share data between AngularJS controllers

    【讨论】:

      【解决方案2】:

      从存储中删除记录后,您可以将更新的对象数组分配给 ng-repeat 中使用的数组

      $scope.remove = function(){
      
          $scope.winesFromStorage = updatedLocalStorageObject;
      
      }
      

      这样您就不必重新加载页面,并且由于它是双向绑定的,它会自动重新加载数据部分。

      【讨论】:

      • 这应该可以。但是,我的页面与表格一起加载到 /wines/ 上,当按下删除时,它会指向 /wines/delete/1。所以看起来它停留在 /wines/ 上,并且没有更新数据。有任何想法吗?我比上面那个更喜欢这个主意!
      猜你喜欢
      • 1970-01-01
      • 2012-07-01
      • 2016-03-02
      • 2018-09-16
      • 1970-01-01
      • 2015-12-22
      • 2012-07-04
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多