父指令中的 angularjs 函数没有从嵌入的 html 中调用 [英] angularjs function in parent directive not getting called from transcluded html

查看:23
本文介绍了父指令中的 angularjs 函数没有从嵌入的 html 中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 angualrjs 指令创建了一个类似功能的下拉列表,该指令在某种程度上起作用,但不是我预期的正确方式.
以下是我面临的问题.

  • 下拉列表的对齐方式对于静态来说是正确的,但对于动态来说却不是
  • 我无法选择任何选项列表,因为我在父指令中定义的 getValue 函数不是从被嵌入的指令中调用的

谁能告诉我一些解决方案

我的代码如下

PLUNKER

静止的<grant-parent ng-model="grand1"><parent label="Parent1" value="12"><child value="56" label="Child1">Child1</child><child value="57" label="Child2">Child2</child></父母><parent label="Parent1" value="13"><child value="58" label="Child3">Child3</child><child value="59" label="Child4">Child4</child></父母></grant-parent>动态的<grant-parent ng-model="grand2"><parent ng-repeat="parent in data" label="{{parent.parentName}}" value="{{parent.parentId}}"><child ng-repeat="child in parent.childrens" label="{{child.name}}" value="{{child.id}}">{{child.name}}</child></父母></grant-parent>

解决方案

Transclusion 和 ng-repeat 让我很头疼,我认为这会很有挑战性,但解决方案很简单:

从链接函数中移除 DOM 操作并在模板中进行嵌入!

<div ng-transclude></div>parent 的模板中并删除这一行: elm.find('div').replaceWith(transclude()).

分叉的 plunk:http://plnkr.co/edit/UGp6D29yxnu0uJwD1BM2?p=preview

<小时>

现在标记出来的有点不同,包装器

仍然存在.虽然看起来没有视觉上的差异,但这可能不是你想要的.我不认为有一个合理的方法来解决这个问题,所以我建议稍微改变一下布局:你为什么不把孩子们放在 inside 父母 <li>,例如如:

  • <b><a href='#' ng-click='getValue(optGroupLabel,optGroupValue)'>{{optGroupLabel}}<span class='value'>{{optGroupValue}}</span>;</a></b><div ng-transclude></div><!-- div 现在在 li 内-->
  • 这在 plunker 中有效,但标记仍然无效(

  • 中).

    最好的解决方案是将孩子们包裹在他们自己的

    查看全文
  • 登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆