如何将ENV变量的值注入Angular模板?

时间:2016-01-09 10:55:09

标签: angularjs ruby-on-rails-4 heroku environment-variables

在我的网络应用中,当我检查Heroku上的结算页面的HTML时 - 表单的hide()属性为空,但在我的本地计算机上(在开发和生产模式下)它正确指向到ENV ['TWOCHECKOUT_PURCHASE_URL']变量的值。

表单的action属性必须包含不同的URL,具体取决于Rails正在运行的环境。如何使它工作?

application.yml

action

home.html.erb

TWOCHECKOUT_PURCHASE_URL: 'https://sandbox.2checkout.com/checkout/purchase'

sbBillingDirective.js.erb

<sb-billing></sb-billing>

billing_directive.html.erb

//= depend_on_asset "billing_directive.html"

(function() {

    angular
        .module('scrumban.widgets')
        .directive('sbBilling', billingDirective);

    function billingDirective() {
        return {
            restrict: 'EA',
            scope: {},
            replace: true,
            templateUrl: '<%= asset_path("billing_directive.html") %>',
            controller: BillingController,
            controllerAs: 'vm'
        }
    }

    BillingController.$inject = ['$scope', 'BillingService', '_'];
    function BillingController($scope, BillingService, _) {
      // not relevant code
    }

1 个答案:

答案 0 :(得分:2)

在这里找到了答案: Access Rails env variable from Angular Controller

我添加.html_safe之后就可以了:

<form id="2checkout" 
      action="<%= ENV['TWOCHECKOUT_PURCHASE_URL'].html_safe %>"
      method="post">
</form>