gpt4 book ai didi

AngularJS 如何加载模板宽度 Controller 并重用它?

转载 作者:行者123 更新时间:2023-12-04 08:53:00 25 4
gpt4 key购买 nike

我有这个代码:

<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>

但是如果你看到它对所有 subview 都是相同的规则,我需要重用它,我该怎么做?

<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 更改要加载的上下文。

谢谢大家!

最佳答案

您不需要使用动态 Controller 。使用模板将代码放入指令中。您的代码可能如下所示(例如):

<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>

每个指令都有自己的 Controller (因此不需要动态分配它们)加上模板 url,这样就消除了 ng-include。您可以尝试使用 ng-if 的方式。您是要使用三个不同的变量并在 Controller 中为它们分配 true/false,还是像您在示例中那样进行检查。

关于AngularJS 如何加载模板宽度 Controller 并重用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41346252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com