![](/img/trans.png)
[英]What is the best place to store ng-templates in Angular/Django SPA
[英]Best place to store angular files in rails
我打算將當前的應用程序轉換為角度應用程序。 以前,我從來沒有機會使用角度和導軌。 我正計划在Rails應用程序功能中逐個實現角度。 但是我仍然擔心文件夾的結構。
關於將角度文件保存在rails應用程序中的位置是否有約定?
棱角型也適合這種開發(逐個功能)還是我應該研究其他技術?
FDD(通過功能開發獲得的功能)在MVC框架中運行良好,而Angular就是其中之一。
通常建議將JS文件(任何JS文件,而不僅僅是Angular)存儲在app/assets/javascripts
以便Rails資產管道自動將它們拾取。 除此之外,沒有約定,將它們存儲在您認為合適的任何結構中。
我之所以這么說,是因為如果Angular代碼變大,最好將其從app
文件夾中提取出來。 然后,您將必須配置資產管道以使用新結構。
對於小型項目,我喜歡將Angular javascript文件保存在app/assets/javascripts/angular
app/assets/javascripts/angular
/controllers/
/directives/
/helpers/
/models/
/services/
apps.js
templates.js.erb
視圖在app/assets/templates
。
我遇到的一大障礙是使Angular視圖與Rails的資產管道一起使用。 在開發中,您什么都沒注意到,但是一旦在生產環境中進行部署,並且啟動了資產管道,Angular就無法再找到視圖了。
為了使資產管道能夠服務於AngularJS視圖,我必須創建一個模板緩存。
// templates.js.erb
(function(){
'use strict';
// version 0.01
// increase this every-time you deploy to production
angular.module('templates', []).run(['$templateCache', function($templateCache) {
<%
environment.context_class.instance_eval { include ActionView::Helpers::JavaScriptHelper }
app_root = File.expand_path('../../../', __FILE__)
templates = File.join(app_root, %w{templates ** *.html})
Dir.glob(templates).each do |f|
key = f.gsub(%r(^#{app_root}/templates/), '')
html = File.read(f).squish
%>
$templateCache.put("<%= key %>", "<%= escape_javascript(html) %>");
<% end %>
}]);
}());
然后,您必須在模塊中注入templates
angular.module("myModule", ['templates']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.