Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度指令使用两次,覆盖元素_Javascript_Jquery_Html_Angularjs_Twitter Bootstrap - Fatal编程技术网

Javascript 角度指令使用两次,覆盖元素

Javascript 角度指令使用两次,覆盖元素,javascript,jquery,html,angularjs,twitter-bootstrap,Javascript,Jquery,Html,Angularjs,Twitter Bootstrap,我正试图为情态动词编写一个简单的指令,以便在我提交情态动词内部的表单时将其关闭 我有两个模态:Loginmodal和signup模态,分别在我的html中排序 modals在modals.html中,该文件在html文件中使用ng include调用,该文件也在ng include中调用,因此我们这里有一个childscope的childscope 我的指令 我不知道为什么链接中的元素会被最后一个元素覆盖 我将模态指令应用于两个模态,但我打开了第一个模态 非常感谢您的帮助您确定scope.$p

我正试图为情态动词编写一个简单的指令,以便在我提交情态动词内部的表单时将其关闭

我有两个模态:Loginmodal和signup模态,分别在我的html中排序

modals在modals.html中,该文件在html文件中使用ng include调用,该文件也在ng include中调用,因此我们这里有一个childscope的childscope

我的指令

我不知道为什么链接中的元素会被最后一个元素覆盖

我将模态指令应用于两个模态,但我打开了第一个模态

非常感谢您的帮助

您确定scope.$parent.$parent.$parent与您的两个modals不同吗?如果它们具有相同的GrandOld父级,则只有最后一个将实际定义grandOldParent.Disclease函数

老实说,当我看到这个范围时,我有点害怕。$parent。$parent。$parent的事情。通常建议避免使用$parent,因此链接其中3个更疯狂

如果您实际操作它的上父级,您真的需要一个独立的作用域:{}?为什么不使用继承的作用域

可能有一种更干净的方法来处理这些范围。您是否正在使用,或者您是否已经查看了使用$modal服务对模态进行角度化的位置,您在该服务上定义了模板和控制器

这也是无用的:

if (!scope.$$phase && !scope.$root.$$phase)
  scope.$parent.$parent.$parent.$apply();
});
应该由

if (!scope.$root.$$phase) {
  scope.$apply();
}
如果此事件是异步的且超出角度摘要周期,则使用$apply

还要注意,scope.$apply实际上是一个$rootScope.$digest,所以您应用哪个作用域并不重要

更新:您可以在指令html中添加ng click=close,并将其添加到其链接函数中:

scope.close = function() {
  // do stuff on this directive instance
  console.log(element.hasClass('in'));

  // close the modal
  var parent = scope;
  while (!parent.dismiss) { parent = parent.$parent; }
  parent.dismiss();
}

这样,指令的每个实例都有自己的close函数,因此不会覆盖任何其他实例,但仍会调用属于每个实例使用的一个模式的唯一Disclose。

我知道使用$parent.$parent.$parent很难看。我内心的开发者告诉我这是废话,应该有更简单的东西。我不认为两个模态的作用域$parent.$parent.$parent是不同的,因为它们在同一个文件中。我将尝试修复内容,然后返回给您。对于编辑,Angular 1.3Ok尚未提供,那么如何使指令不覆盖第一个模式?感谢您的时间,我正在使用angular 1.3版本的angular bootstrap的旧版本,甚至尝试了1.4.0-beta.6版本,它工作起来很有魅力。它是一种代码方式清理器,您只在实际需要时加载一个模式模板,并使用它自己的控制器。我不知道您试图通过重写这个父dimis方法来做什么。你就不能写scope.dimiss=?尝试使用scope:true而不是scope:{}
scope.close = function() {
  // do stuff on this directive instance
  console.log(element.hasClass('in'));

  // close the modal
  var parent = scope;
  while (!parent.dismiss) { parent = parent.$parent; }
  parent.dismiss();
}