我刚开始在我的Rails应用程序中使用AngularJS,因为我习惯于在Rails中使用Haml模板,所以我希望在客户端中使用AngularJS时也能这样做。问题是我不知道在哪里读取Haml文件。
我有一个投资者模型,我正在尝试将“show”模板转换为Haml,因为它是最容易入手的。
这是与show相关的我的AngularJS代码:
investors.js.coffee
# Setup the module & route
angular.module("investor", ['investorService'])
.config(['$routeProvider', ($provider) ->
$provider
.when('/investors/:investor_id', {templateUrl: '/investors/show.html', controller: 'InvestorCtrl'})
])
.config(["$httpProvider", (provider) ->
provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content')
])
angular.module('investorService', ['ngResource'])
.factory('Investor', ($resource) ->
return $resource('/investors/:investor_id.json', {}, {
show: {method: 'GET'},
})
)
angular.bootstrap document, ['investor']
这是我的AngularJS控制器代码
investors_controller.js.coffee
# Show Investor
window.InvestorCtrl = ($scope, $routeParams, Investor) ->
html = haml.compileHaml({sourceId: 'simple'})
$('#haml').html(html())
investor_id = $routeParams.investor_id
$scope.investor = Investor.show({investor_id: investor_id})
我在后端有一个Rails JSON API。
这是它读取的show.html文件
<script type="text/haml-template" id="simple"></script>
<div id="haml"></div>
<h1>{{investor.name}}</h1>
<p>
<b>Total Cost</b>
{{investor.total_cost}}
</p>
<p>
<b>Total Value</b>
{{investor.total_value}}
</p>
<a href='/investors/angular#/investors/{{investor.id}}/edit'>Edit</a>
<a href='/investors'>Back</a>
最终我想要一个.haml文件,并将其传递给templateURL,在AngularJS开始查看动态字段并更改内容之前,让haml_coffee_assets插件对其进行编译。
参考:https://github.com/netzpirat/haml_coffee_assets 目前这会将HAML转换并放入ID为haml的div中。但是,由于操作太晚,AngularJS无法将HAML代码中的{{investor.name}}更改为投资者的姓名。
如何在类似于此类AngularJS项目中正确实现客户端HAML模板?