在异步回发范围函数上获取JavaScript错误

时间:2013-08-13 05:19:11

标签: javascript angularjs

我在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运行时错误:'错误'未定义

1 个答案:

答案 0 :(得分:0)

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';
        })
    }
}