简体   繁体   English

使用Express Handlebars和Angular JS

[英]Using Express Handlebars and Angular JS

Background 背景

I am currently building a website that uses NodeJS for the server, Express Handlebars (Just Handlebars but server side) , and hopefully AngularJS for some client side stuff. 我目前正在建立一个网站,使用NodeJS作为服务器, Express Handlebars (Just Handlebars,但服务器端),并希望AngularJS用于某些客户端。


The Problem 问题

AngularJS and Handlebars use the same syntax for templating AngularJS和Handlebars使用相同的语法进行模板化
{{foo}}
This causes a problem where AngularJS code will be interpreted by Express Handlebars first, which will then throw an error because the data it is trying to pull only exists in Angular not Node. 这会导致首先由Express Handlebars解释AngularJS代码的问题,然后会抛出错误,因为它尝试拉取的数据仅存在于Angular而非Node中。


The Question 问题

Is there a way to get AngularJS and Express Handlebars to work together? 有没有办法让AngularJS和Express Handlebars一起工作?


Possible Solutions 可能的解决方案

  • Change the syntax of AngularJS 更改AngularJS的语法
    • I was looking at BackboneJS and it looks like it is possible to change the syntax. 我在看BackboneJS,看起来可以改变语法。 There could possibly be something similar is AngularJS. AngularJS可能会有类似的东西。
  • Create a ng helper in Express Handlebars. 在Express Handlebars中创建一个ng帮助器。
    • It would just return its un-parsed content. 它只会返回未解析的内容。 However I couldn't figure out how to do this. 但是我无法弄清楚如何做到这一点。

Your first solution is possible, AngularJS allow to change the start/end symbols of text interpolation like this: 你的第一个解决方案是可能的,AngularJS允许更改文本插值的开始/结束符号,如下所示:

appModule.config(function($interpolateProvider) {
  $interpolateProvider.startSymbol('{[{');
  $interpolateProvider.endSymbol('}]}');
});

Then you could use it in your template: 然后你可以在你的模板中使用它:

<div>{[{message}]}</div>

Also see: $interpolateProvider documentation 另请参阅: $ interpolateProvider文档

Hope this helps. 希望这可以帮助。

You can always use the ng-bind syntax instead: 您始终可以使用ng-bind语法:

<p ng-bind="user.profile.description"></p>
This is identical to 这与之相同
<p>{{user.profile.description}}</p>

From the Angular docs for ngBind : 来自ngBind的Angular文档:

Typically, you don't use ngBind directly, but instead you use the double curly markup like {{ expression }} which is similar but less verbose. 通常,您不直接使用ngBind,而是使用类似{{expression}}的双卷曲标记,它类似但不那么冗长。

It is preferable to use ngBind instead of {{ expression }} if a template is momentarily displayed by the browser in its raw state before Angular compiles it. 如果在Angular编译它之前浏览器在其原始状态下暂时显示模板,则最好使用ngBind而不是{{expression}}。 Since ngBind is an element attribute, it makes the bindings invisible to the user while the page is loading. 由于ngBind是一个元素属性,因此在加载页面时,它会使绑定对用户不可见。

In order to maintain the AngularJS Style, your second solution is better, Create a helper in Express Handlebars. 为了保持AngularJS Style,你的第二个解决方案更好, 在Express Handlebars中创建一个帮助器。

References to the Handlebars Web Site: http://handlebarsjs.com/block_helpers.html , you can register a helper raw-helper 参考Handlebars网站: http//handlebarsjs.com/block_helpers.html ,你可以注册一个助手raw-helper

Handlebars.registerHelper('raw-helper', function(options) {
    return options.fn();
});

and use it in your hbs template by putting it in a quadruple-stash {{{{ 并通过将其放入四重藏匿 {{{{并在您的hbs模板中使用它

{{{{raw-helper}}}}
<div class="container" ng-controller="AppCtrl">
    Total Members: {{members.length}}
</div>
{{{{/raw-helper}}}}

There is a better way: \\{{foo}}. 有一种更好的方法:\\ {{foo}}。 Handlebars content may be escaped in one of two ways, inline escapes or raw block helpers. 车把内容可以通过两种方式之一进行转义:内联逃生或原始帮助。 Inline escapes created by prefixing a mustache block with \\ . 通过使用\\前缀胡子块创建的内联转义。 Raw blocks are created using {{{{ mustache braces. 使用{{{{mustache braces。 You can see this http://handlebarsjs.com/expressions.html#helpers 你可以看到这个http://handlebarsjs.com/expressions.html#helpers

I would recommend using the AngularJS's data-binding syntax (what looks similar to Handlebars) and have your NodeJS server simply serve the static AngularJS source code. 我建议使用AngularJS的数据绑定语法(类似于Handlebars),让NodeJS服务器只提供静态AngularJS源代码。 I prefer to offload the templating onto the client and therefore put less stress on your server, not to mention that AngularJS and other MVC frameworks (my favourite is EmberJS) are great for dynamically building the webpage. 我更喜欢将模板卸载到客户端上,因此减轻了服务器的压力,更不用说AngularJS和其他MVC框架(我最喜欢的是EmberJS)非常适合动态构建网页。

I am a fan of Yeoman and here is a generator for building an Angular project served by NodeJS: https://github.com/yeoman/generator-angular 我是Yeoman的粉丝,这里有一个用于构建NodeJS服务的Angular项目的生成器: https//github.com/yeoman/generator-angular

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

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