簡體   English   中英

在異步回發范圍函數上獲取JavaScript錯誤

[英]Getting JavaScript error on asynchronous postback scope function

我在angular.min.js上報告了以下錯誤

0x800a1391-JavaScript運行時錯誤:“錯誤”未定義

使用以下代碼:

Javascipt:

function Sucess() 
{
    //close
}

function Save() 
{
    var e = document.getElementById('FormDiv');
    scope = angular.element(e).scope();
    scope.Apply(Sucess)
}

我的角度范圍函數:

function RolesCtrl($scope, $http, $location) 
{
    $scope.Apply = function (CallBackSucess) {

    var userId = getQSP('uid', decode(document.URL));
    $http({
    method: 'POST', url: 'MultiRole.aspx/Apply',
    data: {}
    }).
    success(function (data, status, headers, config) {
        // this callback will be called asynchronously
        CallBackSucess();
        $scope.f = data.d;
        }).
    error(function (data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    $scope.name = 'error';
    })
    }
}

在調用CallBackSucess()引發錯誤之前,一切似乎都工作正常:

0x800a1391-JavaScript運行時錯誤:“錯誤”未定義

CallBackSucess參數傳遞給.Apply()方法。 它不會傳遞給.success()方法-它們是具有單獨范圍的鏈接方法。 因此,在.success()回調函數中,當您嘗試調用CallbackSucess()時未定義它,從而導致錯誤。

另外,您真的要錯誤地拼寫Sucess嗎?

僅供參考,為了查看實際情況,我必須像這樣格式化您的代碼:

function RolesCtrl($scope, $http, $location) {
    $scope.Apply = function (CallBackSucess) {
        var userId = getQSP('uid', decode(document.URL));
        $http({
            method: 'POST', 
            url: 'MultiRole.aspx/Apply',
            data: {}
        }).success(function (data, status, headers, config) {
            // this callback will be called asynchronously
            CallBackSucess();
            $scope.f = data.d;
        }).error(function (data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
            $scope.name = 'error';
        })
    }
}

暫無
暫無

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

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