簡體   English   中英

在postj響應后重新加載數據

[英]After post response reload data in 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); 
    } 
}) 

圖片提交后 提交后

如果我做手冊頁實時數據,則僅顯示數據[ 手冊頁重新加載[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 = {}; 
    });
} 

$ scope.addDim函數的末尾添加$ scope.dims = Dim.index() 它將撥打電話,您將獲得數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM