Using Express Handlebars and Angular JS

前端 未结 5 630
南笙
南笙 2020-11-30 21:01

Background

I am currently building a website that uses NodeJS for the server, Express Handlebars(Just Handlebars but server side) , and hopefully AngularJS for som

相关标签:
5条回答
  • 2020-11-30 21:15

    You can always use the ng-bind syntax instead:

    <p ng-bind="user.profile.description"></p>
    This is identical to
    <p>{{user.profile.description}}</p>

    From the Angular docs for ngBind:

    Typically, you don't use ngBind directly, but instead you use the double curly markup like {{ expression }} which is similar but less verbose.

    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. Since ngBind is an element attribute, it makes the bindings invisible to the user while the page is loading.

    0 讨论(0)
  • 2020-11-30 21:20

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

    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

    0 讨论(0)
  • 2020-11-30 21:23

    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.

    0 讨论(0)
  • 2020-11-30 21:32

    In order to maintain the AngularJS Style, your second solution is better, Create a helper in Express Handlebars.

    References to the Handlebars Web Site: http://handlebarsjs.com/block_helpers.html, you can register a helper 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 {{{{

    {{{{raw-helper}}}}
    <div class="container" ng-controller="AppCtrl">
        Total Members: {{members.length}}
    </div>
    {{{{/raw-helper}}}}
    
    0 讨论(0)
  • 2020-11-30 21:35

    There is a better way: \{{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. You can see this http://handlebarsjs.com/expressions.html#helpers

    0 讨论(0)
提交回复
热议问题