使用 Express Handlebars 和 Angular JS [英] Using Express Handlebars and Angular JS

查看:16
本文介绍了使用 Express Handlebars 和 Angular JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我目前正在构建一个使用 NodeJS 作为服务器的网站,Express 把手(只是 把手但服务器端) ,并希望 AngularJS 用于一些客户端的东西.


问题

AngularJS 和 Handlebars 使用相同的模板语法
{{foo}}
这会导致一个问题,即 AngularJS 代码将首先被 Express Handlebars 解释,然后会抛出错误,因为它试图拉取的数据只存在于 Angular 而不是 Node 中.


问题

有没有办法让 AngularJS 和 Express Handlebars 一起工作?


可能的解决方案

  • 改变AngularJS的语法
    • 我正在研究 BackboneJS,看起来可以更改语法.可能有类似的东西是 AngularJS.
  • 在 Express Handlebars 中创建一个 ng 助手.
    • 它只会返回其未解析的内容.但是我不知道如何做到这一点.

解决方案

您的第一个解决方案是可能的,AngularJS 允许像这样更改文本插值的开始/结束符号:

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

然后你可以在你的模板中使用它:

{[{message}]}

另见:$interpolateProvider 文档

希望这会有所帮助.

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.


The Problem

AngularJS and Handlebars use the same syntax for templating
{{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.


The Question

Is there a way to get AngularJS and Express Handlebars to work together?


Possible Solutions

  • Change the syntax of AngularJS
    • I was looking at BackboneJS and it looks like it is possible to change the syntax. There could possibly be something similar is AngularJS.
  • Create a ng helper in Express Handlebars.
    • 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:

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

Then you could use it in your template:

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

Also see: $interpolateProvider documentation

Hope this helps.

这篇关于使用 Express Handlebars 和 Angular JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆