簡體   English   中英

Angular JS和AJAX發布數據無法正常工作

[英]Angular JS and AJAX posting data doesn't work first click

我專注於Angular JS,我嘗試在ANGULARJS中使用AJAX發布數據,但它尚可,但不是第一次。

在我的html按鈕中有ng-click =“ check()”

這是我的app.js代碼。

    var app=angular.module("app",[]);
app.controller("control",function($scope){

    $scope.check=function (){
    var mesaj = {
        content: $scope.content,
        flag:"message"
    }
    $.ajax(
    {
        type: 'POST',
        url: 'send.php',
        data: {query: mesaj},
        success: function (result)
        {
            $scope.result=result;
        }
    });
    }
});

我的send.php在回顯$ content之后獲取數據(內容)

我需要雙擊以使用AJAX,但是我想在第一次使用

由於您沒有使用http服務,

您需要告訴angular變量已修改。

在成功之后執行$scope.$apply()

var app=angular.module("app",[]);
app.controller("control",function($scope){

    $scope.check=function (){
    var mesaj = {
        content: $scope.content,
        flag:"message"
    }
    $.ajax(
    {
        type: 'POST',
        url: 'send.php',
        data: {query: mesaj},
        success: function (result)
        {
            $scope.result=result;
            $scope.$apply()
        }
    });
    }
});

注入$ http並調用一個帖子,使用angular來發布數據時遵循更好的約定:

 function sendMesaj() {

         $http.post('send.php', mesaj)
            .then(function(result){

                $scope.result=result;

            }, handleError);

        function handleError(response){
            return response;
        }
    }

暫無
暫無

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

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