简体   繁体   English

Angular 1指令未在ng-repeat内的指令内呈现

[英]Angular 1 directive does not render inside a directive inside ng-repeat

I'm having issues with Angular directives. 我在Angular指令方面遇到问题。

My goal is to render a directive mmContentRow for each element in a scope using ng-repeat . 我的目标是使用ng-repeat为范围中的每个元素呈现指令mmContentRow That directive mmContentRow has a template where another directive relativeDate is rendered. 该指令mmContentRow具有一个模板,在该模板中呈现了另一个指令relativeDate

The problem is that relativeDate does not get rendered inside mmContentRow . 问题在于, relativeDate不会在mmContentRow内部mmContentRow I've tried many solutions but nothing so far. 我已经尝试了许多解决方案,但到目前为止没有任何尝试。 Here is the code: 这是代码:

parent.html : parent.html

  <ul>
    <mm-content-row ng-repeat="report in selected.reports" date="report.reported_date"/>
  </ul>

mm-content-row.js

angular.module('inboxDirectives').directive('mmContentRow', function() {
  return {
    restrict: 'E',
    templateUrl: 'mm-content-row.html',
    scope: {
      date: '=',
    }
  };
});

mm-content-row.html : mm-content-row.html

<li>
  <span>{{date}}</span>
  <relative-date date="{{date}}"></relative-date>
</li>

relative-date.js : relative-date.js

angular.module('inboxDirectives').directive('relativeDate', ['FormatDate', function(FormatDate) {
  return {
    restrict: 'E',
    template: '<span>rendered something</span>',
    scope: {
      date: '=',
    }
  };
}]);

Example data: 示例数据:

{ selected: {reports: 
[{reported_date: 1508493112758}, {reported_date: 1508493101933}]
} }

Rendered output: 渲染输出:

<ul>
  <li>
    <span>1508493112758</span>
    <relative-date date="1508493112758"></relative-date>
  </li>
  <li>
    <span>1508493101933</span>
    <relative-date date="1508493101933"></relative-date>
  </li>
</ul>

Expected output: 预期产量:

<ul>
  <li>
    <span>1508493112758</span>
    <span>rendered something</span>
  </li>
  <li>
    <span>1508493101933</span>
    <span>rendered something</span>
  </li>
</ul>

As far as I can see, the relative-date inside a directive inside ng-repeat doesn't get compiled. 据我所知, ng-repeat内的指令中的relative-date不会被编译。 I expected Angular to automatically compile it, but it doesn't seem to happen. 我希望Angular能够自动编译它,但是似乎没有发生。 Should I explicitly tell Angular to compile relative-date inside mmContentRow ? 我应该明确地告诉Angular在mmContentRow编译relative-date吗?

Update: I've created a fiddle with simplified version of my problem: http://jsfiddle.net/cbrwizard/4e2r2o07/ . 更新:我用问题的简化版本创建了一个小提琴: http : //jsfiddle.net/cbrwizard/4e2r2o07/ Everything works there. 一切正常。 Weird! 奇怪的! I will post here an update if I figure the difference between the fiddle and my code. 如果我知道小提琴和我的代码之间的区别,我将在这里发布更新。

Thanks to @appeiron's suggestion, I've created a fiddle http://jsfiddle.net/cbrwizard/4e2r2o07/ which let me understand that the issue is in Angular, but in the project's code. 感谢@appeiron的建议,我创建了一个小提琴http://jsfiddle.net/cbrwizard/4e2r2o07/ ,让我了解到问题出在Angular,但在项目的代码中。

Turned out a template wasn't rendered directly, but instead some regex magic was used to parse the html file. 原来,模板不是直接呈现的,而是使用了一些正则表达式魔术来解析html文件。 Adding 新增中

$compile($(element).contents())(scope)

where element is an html file string was enough to make it work. 其中element是一个html文件字符串足以使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM