我有这段代码:
<div class="mdl-cell mdl-cell--5-col mdl-shadow--4dp" style="margin-bottom:0;" ng-show="sub.view == 'fundoCaixa'">
<div ng-if="sub.view=='fundoCaixa'" ng-controller="vender-fundoCaixa">
<div ng-include="'views/vender/fundoCaixa.html'"></div>
</div>
</div>
<div class="mdl-cell mdl-cell--5-col mdl-shadow--4dp" style="margin-bottom:0;" ng-show="sub.view == 'sangria'">
<div ng-if="sub.view=='sangria'" ng-controller="vender-sangria">
<div ng-include="'views/vender/sangria.html'"></div>
</div>
</div>
<div class="mdl-cell mdl-cell--5-col mdl-shadow--4dp" style="margin-bottom:0;" ng-show="sub.view == 'apelido'">
<div ng-if="sub.view=='apelido'" ng-controller="vender-apelido">
<div ng-include="'views/vender/apelido.html'"></div>
</div>
</div>
但是如果你看到所有子视图都是相同的规则,我需要重复使用它,我该怎么做?
<div class="mdl-cell mdl-cell--5-col mdl-shadow--4dp" style="margin-bottom:0;" ng-show="sub.view == '$var_here'">
<div ng-if="sub.view=='$var_here'" ng-controller="$var_here">
<div ng-include="'views/vender/$var_here.html'"></div>
</div>
</div>
$ var_here 更改为要加载的上下文。
谢谢大家!
答案 0 :(得分:1)
您不需要使用动态控制器。将代码放入带模板的指令中。您的代码可能如下所示(例如):
<div class="mdl-cell mdl-cell--5-col mdl-shadow--4dp" style="margin-bottom:0;" ng-show="sub.view == '$var_here'">
<fundo-caixa ng-if="ctrl.fundoCaixa"></fundo-caixa>
<sangria ng-if="ctrl.sangria"></sangria>
<apelido ng-if="ctrl.apelido"></apelido>
</div>
每个指令都有自己的控制器(因此不需要动态分配它们)和模板URL,因此消除了ng-include。 您可以尝试使用ng-if的方式。您是否想要使用三个不同的变量并在控制器中将它们指定为true / false,或者像您在示例中那样进行检查。