简体   繁体   English

无服务器Jade与Angularjs的模板

[英]Serverless Jade templating with Angularjs

I understand client-side template rendering is a controversial subject, but when coding for compatibility with all web servers, it's a necessity. 我了解客户端模板渲染是一个有争议的主题,但是在进行编码以与所有Web服务器兼容时,这是必须的。

I'm after a simple solution for rendering jade templates using angularjs... Here is a over-simplified example: http://jsfiddle.net/BqnR6/ 我正在寻找一个使用angularjs渲染玉器模板的简单解决方案...这是一个过于简化的示例: http : //jsfiddle.net/BqnR6/

angular.bootstrap(document)

The example renders a single page and bootstraps angular to evaluate the elements. 该示例呈现单个页面并引导角度来评估元素。 What I'm after is a solution that uses routing to call up jade files, render them and then evaluate with angularjs. 我所需要的是一个使用路由调用玉石文件,渲染它们然后使用angularjs进行评估的解决方案。

Is there a way or a workaround to achieve this functionality? 是否有方法或解决方法来实现此功能?

Please note that the rendering of the templates needs to be serverless (ie no nodejs). 请注意,模板的呈现必须是无服务器的(即没有nodejs)。

Here's a solution that you embed all your templates into angular ng-template script tags: 您可以将所有模板都嵌入有角ng-template脚本标签的解决方案:

<script type="text/ng-template" id="myTemplate.html" class="angular-template">
       <!-- jade template --></script>

The angular convention for this is the ID of type="text/ng-template" is a valid template URL and if found angular will pull from script tag before it would attempt to get via ajax 为此的角度约定是type="text/ng-template"的ID,是有效的模板URL,如果找到了angular,它将在尝试通过Ajax获取之前从脚本标记中提取

Before bootstrapping angular compile innerHtml of these script tags 在自举这些脚本标记的angular编译innerHtml之前

var templates = document.querySelectorAll('.angular-template')
for (i = 0; i < templates.length; i++) {
  templates[i].innerHTML = jade.compile(templates[i].innerHTML.trim())()
}

Example app using ng-view for routing: 使用ng-view进行路由的示例应用程序:

var app = angular.module('plunker', ['ngRoute']);

app.config(function($routeProvider) {
  $routeProvider.
  when('/', {
    templateUrl: 'myTemplate.html' /* note is same as script tag ID above*/
  });

});

app.controller('testApp', function($scope){
  $scope.title="See script tag for template";
   $scope.numbers = [1,2,3,4,5]
})

angular.bootstrap(document, ['plunker'])

DEMO DEMO

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

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