Is it possible / sensible to use external templating engine like dust.js or any other with anjularjs?

*爱你&永不变心* 提交于 2019-12-24 04:35:28

问题


I had this urge to use dust.js templates as it provides a much better performance for UI rendering by caching the templates.

But in my current project we are using angularjs. It is even possible/sensible to use dust.js or any other templating engine with angular js ??

Even if i use dust.js will I lose the 2-way binding .. ?

Please suggest considering a relatively large SPA.. ?

P.S. I am a novice in both angular and dust.


回答1:


Sounds like a good use case for a filter!

Be aware that dust.js is an asynchronous renderer, but if you've already loaded everything then dust will fire synchronously (most of the time)

app.module('yours',[]).filter('dustRender', function(){
  return function(input, templateName){
    var rendered;
    dust.render(templateName, input, function(err, out){
      if('string' === typeof out){
        rendered = out;
      }
      err && console.error('Dust rendering error!', err);
    });
    return rendered || input;
  };
});

template

<span>{{ modelData | dustRender:'registered-dust-template' }}</span>

Note: Angular $sanitizes output, like html.



来源:https://stackoverflow.com/questions/18843501/is-it-possible-sensible-to-use-external-templating-engine-like-dust-js-or-any

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!