1

所以,我有一个与salesforce集成的角度代码。它向 salesforce 打了一个休息电话以获取一些记录。

现在在那个休息调用的成功处理程序中,我试图用一个单独的控制器打开一个新的 div,我得到了错误:

TypeError:无法调用 null 的方法“addEventListener”

如果我尝试从成功处理程序之外调用它,我不会收到此错误。但是由于我的数据在我的成功处理程序中被查询,我需要在那里使用它。

这是我的代码:

    filterservice.getproductlist(successHandler,errorHandler);

function successHandler(data) {
    $scope.$apply(function () {
        defer.resolve(data);
    });
}

defer.promise.then(function(data){
    console.log('In not');
   filterservice.setproductlist(data);
}).then(function(){
        console.log('open then');
        $scope.opendetailwindow(
        'sellingAreas/sellingAreasFilters/selectProducts.tpl.html',
        'showProducts');
});
4

1 回答 1

0

what that means is the element that you're attaching events to was not found.

consider this jquery:

$('.something').on('click', doSomeCallback );

This code will return an array, and for each array item, it will apply doSomeCallback as the callback function. This is how jquery protects the code writer from having null errors when the array has a length of zero.

In pure javascript, you'd say something like this:

var element = document.getElementById('someElement');
element.addEventListener('click', doSomeCallback);

But if element is not found, then element will equal null, which then has no properties or class methods... one of which would be addEventListener.

In your code, you're using defer which is presumably built on top of the addEventListener method in javascript.

Thus, whatever element you attached your defer events / methods to is not always being found in the DOM. so make sure that you figure out why that element is not being found, and that will stop you from having that error.

于 2013-09-09T20:04:07.057 回答