如何在angularjs中执行更新(PUT)调用?

时间:2015-04-21 05:18:35

标签: angularjs crud

我是angularjs的新手。

我有以下UI模板 DashBoard.html enter image description here

点击创建新活动我将数据发布到服务器。

在上面的屏幕截图中, => 编辑功能。

点击我在UI模板下方显示的铅笔符号。 管理-CMS.html enter image description here

现在我想用预填充值填充那些空文本框。

并点击SAVE按钮我想将该数据更新到服务器。

我怎么能实现这个目标?

请帮忙。

我正在尝试的代码:

codeApp.controller('DashboardController', function($scope, $rootScope, $location, $http) {

    $scope.username = "Admin";

    $scope.apps = [];

    $scope.initController = function(){

        var appDetails = new Array();
        var appObject = new Object();
        $scope.id = sessionStorage.id;

        $http.get('http://192.168.1.30:8090/apps/').
            success(function(data, status, headers, config) {
                console.log(data);

                for(var key in data._embedded.apps){
                    appObject = data._embedded.apps[key];
                    appDetails.push(appObject);
                    $rootScope.appId = data._embedded.apps[key].appId;
                }
                $scope.appDetails = appDetails;
            }).
            error(function(data, status, headers, config) {
                alert("Failed to load app details");
            });
    };

    $scope.go = function (path) {
        $location.path(path);
        var display = false;
        if(!display){

        }
    };

   $scope.addApp = function(){      
        $scope.apps.push({'name':$scope.name, 'domain': $scope.domain, 'appId' : $scope.appId, 'secret' : $scope.secret});
        // Writing it to the server
        //      
        var dataObj = {
                name : $scope.name,
                domain : $scope.domain,
                appId : $scope.appId,
                secret : $scope.secret
        };  
        var res = $http.post('http://192.168.1.30:8090/apps/', dataObj);
        res.success(function(data, status, headers, config) {
            $scope.message = data;
        });
        res.error(function(data, status, headers, config) {
            alert( "failure message: " + JSON.stringify({data: data}));
        });     
        // Making the fields empty
        //
       $scope.name='';
       $scope.domain='';
       $scope.appId = '';
       $scope.secret = '';
    };

});

注意:同一个SAVE按钮用于服务器端功能,即POST和PUT

1 个答案:

答案 0 :(得分:0)

您可以通过单击编辑按钮传递该行的数据,最简单的方法是使用模态(我更喜欢使用基于bootstrap的angular-bootstrap by angular-ui https://github.com/angular-ui/bootstrap)。 将ng-repeat行的数据传递给模态,然后更新表单中的数据。保存时,您可以调用$ http.put / post来将数据更新到服务器。