Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不刷新网页的情况下显示数据库中保存的输入值?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在不刷新网页的情况下显示数据库中保存的输入值?

Javascript 如何在不刷新网页的情况下显示数据库中保存的输入值?,javascript,angularjs,Javascript,Angularjs,我编写了一个代码,使我能够使用AngularJS 1.3.5$http.post发布输入值,并将其保存到数据库中,然后在我的html页面中显示它。要在单击保存后获得新的输入值,我必须刷新页面以显示它。我必须找到一个不使用php和jQuery的解决方案。我看到了一个答案:从$http.post更改为$xhr.post是不可能的,可能是我使用的angularJs版本造成的。我该怎么办 <form ng-submit="save()"> <input ng-model="stac

我编写了一个代码,使我能够使用AngularJS 1.3.5$http.post发布输入值,并将其保存到数据库中,然后在我的html页面中显示它。要在单击保存后获得新的输入值,我必须刷新页面以显示它。我必须找到一个不使用php和jQuery的解决方案。我看到了一个答案:从$http.post更改为$xhr.post是不可能的,可能是我使用的angularJs版本造成的。我该怎么办

<form ng-submit="save()">
  <input ng-model="stack"></input>
  <button type="submit">Save</button>
<p>{{stack}}</p> 
</form>
$scope.save = function(url, data1) {
            /* post to server */
            $http({
                url : url,
                data : {
                    "stack" : data1
                },
                method : "POST",
                headers : {
                    'Content-Type' : 'application/json'
                }
            }).success(function(data, status, headers, config) {
                $scope.stack = data.stack;
            }).error(function(data, status, headers, config) {
                $scope.status = status + ' ' + headers;
            });

        };

请注意,我正在从后端在html页面中显示输入值。

步骤1:使您的请求返回新数据

步骤2:使用该数据替换旧数据

因为你没有提供太多关于结构如何的信息,所以我会尽量做到泛型。假设您正在更新产品并获取其新价格:

$scope.product = {
    name: 'Pop Rocks',
    price: 1.99 // can be changed from an input with `ngModel` or something
};

$scope.save = function() {
    // post new price
    $http.post('/api/products', $scope.product).then(function(response) {
        // response.data contains the JSON object for he product,
        // with new price/name/etc
        product = JSON.parse(response.data) // parse it into an object
        $scope.product = product // replace old $scope.product with new product
    });
}

$scope.doublePrice = function() {
    // using this will update the new price immediately -in the view-,
    // just to show you how binding works. It will not update DB
    $scope.product.price *= 2;
}
假设您的观点与此类似:

<strong>Price:</strong> {{ product.price | currency:"$" }}

由于双向绑定,您的更改应该能够反映出来

步骤1:让您的请求返回新数据

步骤2:使用该数据替换旧数据

因为你没有提供太多关于结构如何的信息,所以我会尽量做到泛型。假设您正在更新产品并获取其新价格:

$scope.product = {
    name: 'Pop Rocks',
    price: 1.99 // can be changed from an input with `ngModel` or something
};

$scope.save = function() {
    // post new price
    $http.post('/api/products', $scope.product).then(function(response) {
        // response.data contains the JSON object for he product,
        // with new price/name/etc
        product = JSON.parse(response.data) // parse it into an object
        $scope.product = product // replace old $scope.product with new product
    });
}

$scope.doublePrice = function() {
    // using this will update the new price immediately -in the view-,
    // just to show you how binding works. It will not update DB
    $scope.product.price *= 2;
}
假设您的观点与此类似:

<strong>Price:</strong> {{ product.price | currency:"$" }}

由于双向绑定,您的更改应该能够反映出来

在函数内绑定get调用,并在同一控制器内调用该函数,这将更新数据而不刷新

  $scope.getData = function() {
        $http({
            method : 'GET',
            url : '/getData'         //any url
        }).then(function(response) {
            //success code

        })
    }
    $scope.getData();

将get调用绑定到一个函数中,并在同一个控制器中调用该函数,这将在不刷新的情况下更新数据

  $scope.getData = function() {
        $http({
            method : 'GET',
            url : '/getData'         //any url
        }).then(function(response) {
            //success code

        })
    }
    $scope.getData();


请给我们更多的代码,像你的控制器代码…新的输入值不是和你之前发送的相同吗,你只能显示它。否则,在save的成功处理程序中进行新的ajax调用call@ArnaudGueras我更新了问题data.stack返回的数据是什么?你确定它有更新的数据吗?@BonMacalindong data.stack返回要发布到后端的输入值请给我们更多的代码,就像你的控制器代码一样…新的输入值不是和你之前发送的相同吗,你只能显示它。否则,在save的成功处理程序中进行新的ajax调用call@ArnaudGueras我更新了问题data.stack返回的数据是什么?您确定它有更新的数据吗?@BonMacalindong data.stack返回要发布到后端的输入值谢谢您的回答。在控制器中调用get方法导致访问允许控制错误。我用我的控制器代码更新了我的问题你是否从后端返回更新的对象?是的,在将其插入数据库后,我使用select请求从数据库调用它,并在web服务中获取它,最后显示它,因此你正在前端进行get调用?你能给我看一下get调用吗?修改类似这个app.factory'myapp',['$http',function$http{return{method:function$scope{$http.get'Url.successfunctiondata{$scope.stack=data;}.errorfunctionerr{$scope.stack=err;};}};在你的控制器中这样调用它,并将$scope注入到它myapp中。方法$scope谢谢你的回答。在控制器中调用get方法导致访问允许控制错误。我用我的控制器代码更新了我的问题你是否从后端返回更新的对象?是的,在将其插入数据库后,我使用select请求从数据库调用它,并在web服务中获取它,最后显示它,因此你正在前端进行get调用?你能给我看一下get调用吗?修改类似这个app.factory'myapp',['$http',function$http{return{method:function$scope{$http.get'Url.successfunctiondata{$scope.stack=data;}.errorfunctionerr{$scope.stack=err;};}};方法$scope谢谢你的回答,我试着按照步骤操作,但仍然有同样的问题。我用控制器代码更新了我的问题。你的data.stack是否真的保存了你需要的数据?它应该会起作用。尝试用$timeout调用$timeoutfunction{$scope.stack=data.stack}包装分配。是的,它应该保存要发布到我的web服务的数据。我尝试了$timeout,但仍然存在相同的问题谢谢你的回答我尝试了按照步骤操作,但仍然存在相同的问题。我用控制器代码更新了我的问题。你的data.stack是否真的保存了你需要的数据?它应该会起作用。尝试用$timeout调用$timeoutfunction{$scope.stack=data.stack}包装分配。是的,它应该保存要发布到我的web服务的数据。我尝试使用$timeout,但仍然存在相同的问题