简体   繁体   English

在postj响应后重新加载数据

[英]After post response reload data in angularjs

Question after I post my data I have a small html table that shows my data however my data doesn't show until I reload the page any help getting this to automatically show my data after I submit will be greatly appreciated. 我发布数据后的问题我有一个小的html表,显示我的数据但是我的数据没有显示,直到我重新加载页面任何帮助,我提交后自动显示我的数据将非常感谢。

Here is my angular_stuff 这是我的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); 
    } 
}) 

Picture After submit 图片提交后 提交后

Data only shows up if I do a manual page realod [ 如果我做手册页实时数据,则仅显示数据[ 手册页重新加载[2]

Dim.save is asynchronous so when you call $scope.dims.push(dim) , dim is not yet saved Dim.save异步的,所以当你调用$scope.dims.push(dim)dim还没有保存

The solution is to use the success callback of the promise 解决方案是使用promise的成功回调

$scope.addDim = function () { 
    Dim.save($scope.newDim).$promise.then(function(dim) {
        $scope.dims.push(dim); 
        $scope.newDim = {}; 
    });
} 

Add $scope.dims = Dim.index() at the end of $scope.addDim function. $ scope.addDim函数的末尾添加$ scope.dims = Dim.index() It will make a call and you will get the data. 它将拨打电话,您将获得数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM