【问题标题】:How to inject value of ENV variable into Angular template?如何将 ENV 变量的值注入 Angular 模板?
【发布时间】:2016-01-09 10:55:08
【问题描述】:

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

Form 的action 属性必须包含不同的 URL,具体取决于 Rails 运行的环境。如何让它发挥作用?

application.yml

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

home.html.erb

<sb-billing></sb-billing>

sbBillingDirective.js.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
    }

billing_directive.html.erb

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

【问题讨论】:

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


    【解决方案1】:

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

    在我添加.html_safe 之后,它起作用了:

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

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 2017-02-18
      • 1970-01-01
      • 2021-11-14
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多