如何在基于Nodejs中的环境variables的Angular中插入dynamic脚本

我在一个Angular应用程序的index.html文件底部有一个脚本,我想根据Node中的一个环境variables进行更改。 我想用一个公共api键和另外一个用于生产。

我使用相同的grunt生成的分期和生产,所以我不知道是否dynamic更改构build常量是build议这里是最好的解决scheme。

有关如何处理这个问题的任何想法?

当环境variables是NODE_ENV = production时,插入这个:

<script> Stripe.setPublishableKey('pk_live_NN4j94VX3mtz2wJtIO3bmH'); </script> 

当环境variables是NODE_ENV = staging时,插入这个:

 <script> Stripe.setPublishableKey('pk_test_LgtEvbZwjC2GaKQYE3I6NnzuA'); </script> 

我会使用grunt-ng-constant来pipe理你的angular度环境variables,然后在你的angular app.config()函数里初始化Stripe,而不是你的文档。

我的首选是使用angular-stripe条纹在angular启动过程中pipe理条纹。 如果使用两个密钥configuration应用程序,这也可能使您的工作更轻松。 以下是我的一个应用程序的一些示例代码。

 var $app = angular.module('app', ['angular-stripe']); $app.constant('devDomains', ['localhost', 'staging.mydomain.com']); $app.constant('stripePubKeyStaging', 'STRIPE_PUB_KEY_HERE'); $app.constant('stripePubKey', 'STRIPE_PUB_KEY_HERE'); $app.config(function(devDomains, stripePubKey, stripePubKeyStaging, stripeProvider) { if (devDomains.indexOf(document.location.hostname) !== -1 || document.location.search.indexOf('stripe-test-key') !== -1) { // if you're running your angular app on local or staging, use the test key. Additionally // if you load your page with ?stripe-test-key you can force your production app // to use the test key in order to debug something on live if you'd like. stripeProvider.setPublishableKey(stripePubKeyStaging); } else { stripeProvider.setPublishableKey(stripePubKey); } });