2015-01-05 89 views
9

我试图在视图加载时显示模态。类型错误:无法读取未定义的属性'childNodes'

代码:

//View 
<div id="showCom" ng-controller="showmodalCtrl" ng-init="init()"> 

<div class="modal fade" id="companyModal" role="dialog"> 
<div class="modal-dialog"> 
    <div class="modal-content"> 
    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
     <h4 class="modal-title">Please Select Company</h4> 
    </div> 
    <div class="modal-body"> 
    <form> 
     <div class="form-group"> 
     <label class="control-label">Recipient:</label> 
     <input type="text" class="form-control"> 
     </div> 
    </form> 
    </div> 
    <div class="modal-footer"> 
     <button type="button" class="btn btn-primary">Submit</button> 
    </div> 
    </div> 
</div> 
</div> 

//Controller 
.controller('showmodalCtrl', ['$scope', function($scope){ 

$scope.init = function(){ 
    $('#companyModal').modal("show"); 
} 
}]) 

一切正常,并显示模式,但我得到的控制台上的错误:

Type error : Cannot read property 'childNodes' of undefined 

我不知道为什么这个正在发生。 此外,如果我从模态中删除“表单”,我不会收到任何错误。所以我想这个问题是与表格有关,但我无法解决它。

在此先感谢。

+0

你包含'jQuery'吗?您正在使用'$('#companyModal')'选择器来选择带有'jQuery'的元素。 –

+0

@MohitPandey ..是的,我包括jquery – Zee

+0

它很难追查这个代码的错误。尝试创建它的http://jsfiddle.net/。此外,内部控制器'''是angular而不是jQuery的引用。尝试使用'jQuery('#companyModal')'。 –

回答

2

抱歉进行编辑。您的HTML格式是否正确?检查所有标签是否匹配。

在你的代码片段中,它看起来像你缺少一个关闭的div。检查 - 关闭控制器div的那个。

39

我有同样的问题,我设法通过在$ timeout()或一个正常的setTimeout()函数内包装模态打开函数来解决它。

setTimeout(function(){ 
    jQuery('#messageModal').modal('show'); 
}, 0); 
+0

这个setTimeout()问题总是存在于Javascript中。我曾经遇到过很多次。无论如何,我从头开始,现在它为我工作,没有超时功能。 – Zee

+1

拯救了我的生命!这应该被标记为接受。 – lbarman

11

当试图使用JQuery插件时,我有同样的错误信息。文档准备好后运行它解决了我的问题。该插件运行良好,但当前范围内的角码不是。

这是“设置超时” - 在这个线程中,让我尝试这个解决方案。

解决方案对我来说:

angular.element(document).ready(function() { 
    //Angular breaks if this is done earlier than document ready. 
    setupSliderPlugin(); 
}); 
2

我也有类似的问题。它会抛出一个异常,当我试图添加一些HTML到DOM并使用$编译在angularJs中编译它。

enter image description here

中HTML的是另一个指令,试图从该页面在页面中添加一些DOM元素到别的地方。

我只是不得不调用该元素的.clone,然后将其移至新位置。 并且例外消失了。

$popupContent = $(attrs.popupSelector).clone(); 
$container = $('<div class="popup-container"></div>'); 
$container.append($popupContent); 
$container.hide(); 
$('body').append($container); 
3

正如AngularJS的背景中另一个解决办法,我使用这种方法:

  1. 包括$timeout模块中的JS控制器
  2. 运行init()功能与所有类似的初始化:

    $ timeout(init,0);

作品不错。

相关问题