角恒定最佳实践 [英] Angular Constant Best Practice

查看:105
本文介绍了角恒定最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有定义Web服务端点的角度恒

  angular.module('MyModule的')。常数('mywebservice_url','http://192.168.1.100)

问题是,对于开发我有一个不同的终点,而分期和生产它的与众不同。每次我试图检查与git我必须手动重置该文件。


  1. 有什么办法混帐permenantly忽略此文件,但检查出来的文件,而克隆结账

  2. 有什么办法可以让动态角头文件从类似环境变量。

请注意:我不想依靠服务器来做到这一点,即我不想用APACH SSI或任何这些技术,因为它会工作只有一组服务器


解决方案

  1. 延迟通过后端处理的注入。我通常只是创建一个名为html页面 pageSettings 哪些值这样的一个全局对象从后端是越来越注入,即环境变量等,并只传递全球 pageSettings 对象成角


  2. 编译系统注入。如果你没有一个后端,即纯净SPA ...也许你可以把这个构建系统内部,即在一饮而尽咕噜,并在构建过程中替换值。


I have an angular constant which defines webservice end point

angular.module('myModule').constant('mywebservice_url', 'http://192.168.1.100')

The problem is that for dev I have a different end point while staging and production its different. Every time I try to check in to git I have to manually reset this file.

  1. Is there any way git permenantly ignore this file but checks out the file while clone or checkout?
  2. Is there any way I can make angular pickup file dynamically from something like environment variable.

NOTE: I don't want to depend on server to do this, ie I don't want to use apach SSI or any of those technologies as it will work only one set of servers.

解决方案

  1. Delaying the injection via backend processing. I usually just create a global object on html page called pageSettings which values like this is getting injected from the backend, i.e. environment variables, etc. and just pass that global pageSettings object into that angular constant or value.

  2. Build system injection. If you don't have a backend, i.e. pure SPA... maybe you can put this inside your build system, i.e. create multiple task for building the different environments in gulp or grunt and replace that value during the build process.

这篇关于角恒定最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆