使用angularJs重新加载页面

时间:2013-07-10 06:50:18

标签: javascript angularjs rest xmlhttprequest angularjs-routing

我是angularJS的新手。我已经开始学习CRUD操作了。我遇到一个问题,当我delete一个条目时,页面应reload

我也经历了$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'
    }); 
});

当行动成功时,我写作:

$location.path('/this');

但是当我执行此操作时,url会从http://localhost/someapp更改为http://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 ... ");
    }
};

在init上我从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

1 个答案:

答案 0 :(得分:0)

您的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 ... ");
    }
};