简体   繁体   English

AngularJS和Rails模板引擎

[英]AngularJS and Rails template engines

As a Rails guy, I love me some Haml or Slim. 作为一个Rails的家伙,我爱我一些Haml或Slim。 However, I haven't had much luck with either when it comes to using AngularJS. 但是,在使用AngularJS时,我也没有太多运气。 Is there a templating engine that is AngularJS friendly and is less verbose than straight HTML? 是否有一个AngularJS友好的模板引擎,并且比直接HTML更简洁?

I'm using slim for my angularjs templates. 我正在使用slim为我的angularjs模板。 It took me forever to figure out how to do it, but its very easy to do once you know how. 我花了很长时间才弄清楚如何去做,但一旦你知道怎么做就很容易做到。

Add the slim gem and bundle install. 添加slim gem和bundle install。

Make sure you have a template directory. 确保您有一个模板目录。 Mine is: 我的是:

assets/javascripts/templates

Then create a file in config/initializers. 然后在config / initializers中创建一个文件。 Mine is just slim_engine.rb. 我只是slim_engine.rb。 The name doesn't matter. 这个名字并不重要。

Then in that file include this line: 然后在该文件中包含以下行:

Rails.application.assets.register_engine('.slim', Slim::Template)

Here's a snippet of the route provider I have that uses one of these templates: 这是我拥有的路由提供程序的片段,它使用以下模板之一:

angular.module('customer_app', ['customer_service'])
  .config(['$routeProvider', function($provider) {
    $provider.when('/:customer_id/edit/', { templateUrl: '/assets/customers/edit.html.slim', controller: 'CustomersController' });
  }])
  .config(["$httpProvider", function(provider) {
    provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
  }]);

The full template path is: assets/templates/customers/edit.html.slim 完整的模板路径是:assets / templates / customers / edit.html.slim

Hope this helps! 希望这可以帮助!

我已经写过如何使用haml为railsjs模板使用haml并在一个请求中正确加载所有模板: http ://minhajuddin.com/2013/04/28/angularjs-templates-and-rails-with- 渴望加载

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

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