在postj响应后重新加载数据

时间:2016-08-08 11:45:15

标签: javascript angularjs

我发布数据后的问题我有一个小的html表格,显示我的数据,但是我的数据没有显示,直到我重新加载页面任何帮助,让我在提交后自动显示我的数据将不胜感激。

这是我的angular_stuff

var dim = angular.module('Dim', ['ngResource']); 


dim.config(['$httpProvider', function($httpProvider) { 
    $httpProvider.defaults.xsrfCookieName = 'csrftoken'; 
    $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; 
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; 
}]); 

dim.config(function ($interpolateProvider) { 
    $interpolateProvider.startSymbol('{[{'); 
    $interpolateProvider.endSymbol('}]}'); 
}); 

dim.factory("Dim", function ($resource) { 


    return $resource("_dims.html", { pk: 'pk' }, { 
        index: { method: 'GET', isArray: true, responseType: 'json' }, 
        update: { method: 'POST', responseType: 'json' }, 
        delete: { method: 'DELETE', headers: { 'X_METHODOVERRIDE': 'DELETE' } }, 
    }); 
}) 


dim.service('dataService', function () { 
    this.data = {} 
    this.data.dimMap = new Array(); 
    this.data.dimMap[""] = "description"; 
}); 


//dim.controller("DimController", function($scope, $http, Dim) { 
dim.controller("DimController", function ($scope, $http, Dim) { 
    $scope.dims = Dim.index() 
    //$scope.dims = dataService.data.dimMap; 

    $scope.addDim = function () { 
        dim = Dim.save($scope.newDim) 
        //My guess is I need something here to reload my data ??? 
        $scope.dims.push(Dim) 
        $scope.newDim = {} 


    } 

    $scope.deleteDim = function (index) { 

        dim = $scope.dims[index] 
        Dim.delete(dim) 
        $scope.dims.splice(index, 1); 
    } 
    $scope.newField = {}; 

    $scope.editDim = function (field) { 
        window.console.log($scope.dims[field]); 
        $scope.editing = $scope.dims.indexOf(field); 
        $scope.newField[$scope.editing] = angular.copy(field); 
    } 
}) 

图片提交后 After Submit

如果我执行手动页面,则仅显示数据 [Manual Page Reload[2]

2 个答案:

答案 0 :(得分:2)

Dim.save 异步,因此当您致电$scope.dims.push(dim)时,dim尚未保存

解决方案是使用promise

的成功回调
$scope.addDim = function () { 
    Dim.save($scope.newDim).$promise.then(function(dim) {
        $scope.dims.push(dim); 
        $scope.newDim = {}; 
    });
} 

答案 1 :(得分:1)

$ scope.addDim 函数末尾添加 $ scope.dims = Dim.index()。它将拨打电话,您将获得数据。