繁体   English   中英

ngRepeat指令的AngularJS服务器端渲染

[英]AngularJS Server-side rendering of ngRepeat directive

从一个我的服务器端变量modules包含一个模板数组的场景来看:

    modules = ["module1.html","module2.html","module3.html"];

假设服务器提供以下内容:

我在这里使用PHP的树枝,但想像一下您可能正在使用的服务器端代码

    {% for module in modules %}
        {% include ("modules/" ~ module) ignore missing %}
    {% endfor %}

这将使我将module1.htmlmodule2.htmlmodule3.html的内容串联在一起。

等效的AngularJS代码为:

    <div 
        ng-repeat="module in modules track by $index" 
        ng-include="'modules/' + module">
    </div>

现在,我想要的是此ngRepeat的服务器端呈现版本。

如果我这样做:

    {% for module in modules %}
        <div 
            ng-repeat="module in modules track by $index" 
            ng-include="'modules/' + module">

        {% include ("modules/" ~ module) ignore missing %}

        </div>
    {% endfor %}

然后,当Angular启动时,页面上将有modules.length * modules.length模块,这是不可接受的。

我如何让Angular 接管该清单,并在其上引入ngRepeat指令,而又不增加结果? 换句话说,我该如何使用服务器端自举数据来呈现ngRepeat指令,而不依赖于angular来进行编译?

更新

我刚刚找到使用模板的部分解决方案

    <div ng-include="'modules'">
        {% for module in modules %}
            {% include ("modules/" ~ module) ignore missing %}
        {% endfor %}
    </div>

    <script type="text/ng-template" id="modules">
        <div 
            ng-repeat="module in modules track by $index" 
            ng-include="'modules/' + module">
        </div>
    </script>

哪种有效,但我正在寻找不同的选择。

尝试使用此npm包,它将呈现任何角度模板:

https://www.npmjs.com/package/ng-node-compile

var ngcompile = require('ng-node-compile');
var ngEnviorment = new ngcompile();
ngEnviorment.$compile("<div ng-repeat=\"n in [1,2,3,4,5]\">hello {{name}} {{n}}</div>")({ name: 'Jhon doe' });

暂无
暂无

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

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