如何防止整页重装?

时间:2014-07-11 10:05:16

标签: jquery ajax angularjs

在我的页面中,我有两个容器。在第一个容器中,我将显示项目列表,相应项目的操作将显示在第二个容器中。

我有一个选项,在运行时我可以通过名为“create item”的链接在第一个容器中添加新项目。

我正在使用以下$ http调用在运行时添加项目

$scope.createItem = function() {
    $http({method:"POST",
        url:"/sample/addItem"
    }).success(function(data) {                     
        window.location.href = "/sample/pages/items.html";
    })              
};

每当我点击“创建项目”链接时,新项目将动态添加到第一个容器中,但整个items.html页面会重新加载。

我需要避免重新加载整个页面。单击链接后,我的第一个容器才会重新加载。

2 个答案:

答案 0 :(得分:0)

jQuery有助于部分加载容器。试试

http://api.jquery.com/load/

.load()有助于仅使用响应加载指定的容器。

$( "#result" ).load( "ajax/test.html" );

答案 1 :(得分:0)

请不要使用 location.href ,您可以使用 $ location 来实现此目的:

function myCtrl($scope, $http,$location){
$scope.createItem = function() {
    $http({method:"POST",
            url:"/sample/addItem"
        }).success(function(data) {                     
           $location.path = "/items";
        })              
    };
}

您的路径应在路线提供商处注册。

相关问题