2013-07-10 139 views
0

我是新手angularJS。我已经开始学习CRUD操作。我遇到一个问题,当我delete条目然后页面应该reload使用angularJs重新加载页面

我也经历了$location$route太。并实施如下:

config

app.config(function ($locationProvider, $routeProvider) { 
    $locationProvider.html5Mode(true); 
    $locationProvider.hashPrefix = '!'; 

    $routeProvider.when('/', { 
     templateUrl: '/views/index.html', 
     controller: 'MainCtrl' 
    }); 
    $routeProvider.when('/this',{ 
     templateUrl: '/views/about.html', 
     controller: 'MainCtrl' 
    }); 
}); 

当action成功那么我的写作:

$location.path('/this'); 

但是当我做到这一点,则url变化从http://localhost/someapphttp://this但页面不刷新。在这种情况下我应该怎么做,请帮助我们吗?

Edit

这是我deletion code

$scope.deletecode = function (id) { 
    name = ''; 
    if (confirm("are you sure to Delete the name")) { 
     $http({ 
      method: 'POST', 
      url: 'rohit.php', 
      data: { 
       "name": name, 
       "id": id, 
       "delete": "true" 
      }, 
     }). 
     success(function (data, status, headers, config) { 
      alert("data deleted successfully"); 
      $location.path('/this'); 
     }). 
     error(function (data, status, headers, config) { 
      alert("Unable to load data."); 
     }); 
    } else { 
     alert("Data deletion cancelled by user ... "); 
    } 
}; 

在初始化我从一个PHP文件中获取的数据:

$http({ 
    method: 'GET', 
    url: 'test.php' 
}). 
success(function (data, status, headers, config) { 
    $scope.samples = data; 
}). 
error(function (data, status, headers, config) { 
    alert("Unable to load data."); 
}); 

所有的数据都存储在$scope.samples返回两件事user_idname

+0

我不确定您是否确实想要刷新页面。删除项目(如果操作正确)将更新视图,而无需刷新页面。 – xbonez

+0

该视图未更新,如何更新视图,我发布了我的删除函数 –

+0

更新了我的代码 –

回答

1

您的http请求将从您的服务器中删除该项目,但您并未从客户端代码中删除该项目,这就是您的视图未更新的原因。您想从客户端代码中删除它,并将其从服务器中删除后:

// all items in an array 
$scope.items = [item1, item2]; 
// delete an item 
$scope.deletecode = function (id) { 
    name = ''; 
    if (confirm("are you sure to Delete the name")) { 
     $http({ 
      method: 'POST', 
      url: 'rohit.php', 
      data: { 
       "name": name, 
       "id": id, 
       "delete": "true" 
      }, 
     }). 
     success(function (data, status, headers, config) { 
      alert("data deleted successfully"); 
      // at this point, the item has been deleted from your server 
      // remove it from $scope.items as well 
      // removes 1 item at index idx 
      $scope.items.splice(idx, 1); 
     }). 
     error(function (data, status, headers, config) { 
      alert("Unable to load data."); 
     }); 
    } else { 
     alert("Data deletion cancelled by user ... "); 
    } 
}; 
+0

嗨什么是idx,在我的情况下是id? –

+0

idx是要删除的数组中项目的索引。你需要跟踪。所以,如果你删除了第一项,'idx = 0'。如果你删除最后一个元素,'idx = $ scope.items.length - 1' – xbonez

+0

我正在从php文件中获取数据并将其存储到'$ scope.samples = data'.it中,它提供了user_id和名称和删除后,我正在做'$ scope.samples.splice(user_id,1)'。这有什么不对? –