我刚刚开始在我的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']
Run Code Online (Sandbox Code Playgroud)
这是我的控制器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})
Run Code Online (Sandbox Code Playgroud)
在后端我有一个Rails JSON API.
这是它读入的show.html文件
<script type="text/haml-template" id="simple">
%h1 {{investor.name}}
</script>
<div …Run Code Online (Sandbox Code Playgroud)