【发布时间】:2017-02-02 01:26:18
【问题描述】:
我有以下 AngularJS 模板文件:
<div ng-controller="PredictionCtrl as predictionCtrl">
<table width="40%" border="1">
<tbody>
<tr>
<td bgcolor="orange">A</td>
<td bgcolor="yellow">B</td>
</tr>
</tbody>
<tfoot>
<tr>
<td bgcolor="pink">C</td>
<td bgcolor="cyan">D</td>
</tr>
</tfoot>
</table>
<br>
<table width="40%" border="1">
<tbody>
<rpt-my-directive-a p="1" q="2"></rpt-my-directive-a>
</tbody>
<tfoot>
<rpt-my-directive-b r="3" s="4"></rpt-my-directive-a>
</tfoot>
</table>
</div>
在我的指令 myDirectiveA 的模板中,我有以下内容:
<tr>
<td bgcolor="orange">E</td>
<td bgcolor="yellow">F</td>
</tr>
在我的指令myDirectiveB 的模板中,我有以下内容:
<tr>
<td bgcolor="pink">G</td>
<td bgcolor="cyan">H</td>
</tr>
输出如下:
我期待两张外观一模一样的桌子。取而代之的是,我得到了一种颜色正确的颜色,而另一种则没有。为什么使用 AngularJS 指令构建的表格看起来与纯 HTML 表格不同?我该如何解决?我希望第二个表格看起来和第一个一样。
编辑#1:
当我在下面接受 Sergey 的建议时,myDirectiveA 的声明现在看起来像这样:
myApp.directive('myDirectiveA',function(){
return {
restrict:'A',
replace: true,
scope: {
p: '=',
q: '='
},
controller: 'MyDirectiveACtrl',
controllerAs: 'MyDirectiveACtrl',
bindToController: true,
template: '<div ng-include="MyDirectiveACtrl.myDirectiveAHTML"></div>'
};
}
);
现在的输出如下所示:
【问题讨论】: