如何将值从node / express app传递给AngularJS控制器

时间:2021-07-10 15:12:36

I'm developing an app based on mean.js boiler plate.

我正在开发一个基于mean.js锅炉板的应用程序。

In one of my Angular controllers, I need to "know" whether the app is running in dev, test, or prod.

在我的一个Angular控制器中,我需要“知道”应用程序是在dev,test还是prod中运行。

When the hosting Node app starts, this is indicated via the NODE_ENV environment variable, so Node knows where its running.

当托管节点应用程序启动时,这将通过NODE_ENV环境变量指示,因此Node知道其运行的位置。

How do I pass this knowledge onto the Angular part of the app?

如何将这些知识传递到应用程序的Angular部分?

2 个解决方案

#1


1  

If you have this knowledge on your backend, why not make an endpoint you can call in an AngularJS run method. While I do not know the details of your backend implementation, surely you can return a variable which represents this information. You can simply craft something like the following...

如果您在后端拥有此知识,为什么不创建一个可以在AngularJS运行方法中调用的端点。虽然我不知道后端实现的细节,但您肯定可以返回表示此信息的变量。你可以简单地制作如下的东西......

app.run(['$rootScope', '$http', function ($rootScope, $http) {
    $http.get('/yourEndoint').success(function(response) {
        $rootScope.whereAmI = response.whereAmI;
    });
}]);

Where wereAmI is some value you return from your backend and return to this call. If you place it on $rootScope, all other $scope's will inherit this value so you'll have knoweldege of "where you're at" app wide.

其中areAmI是你从后端返回的一些值并返回此调用。如果你把它放在$ rootScope上,那么所有其他$ scope都将继承这个值,这样你就可以知道“你在哪里”app app。

For more info on the run function, check out the AngularJS module docs

有关run函数的更多信息,请查看AngularJS模块文档

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the services have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

运行块是Angular中与main方法最接近的东西。运行块是需要运行以启动应用程序的代码。在配置完所有服务并创建注入器后执行。运行块通常包含难以进行单元测试的代码,因此应在隔离模块中声明,以便在单元测试中忽略它们。

#2


1  

In my case, I use gulp/grunt build task to select the requested config.js file (ie. production.config.js). Then you can use something like npm args to set an --env=production or --env=development variable when running your build task, which then is used by the gulp/grunt task to grab the requested config file.

在我的例子中,我使用gulp / grunt构建任务来选择所请求的config.js文件(即.production.config.js)。然后,您可以使用npm args之类的东西在运行构建任务时设置--env = production或--env =开发变量,然后gulp / grunt任务使用它来获取请求的配置文件。

Then your actual config file will just be an angular.constant(...) component with settings to use for your app.

然后你的实际配置文件将只是一个angular.constant(...)组件,其中包含用于你的应用程序的设置。

#1


1  

If you have this knowledge on your backend, why not make an endpoint you can call in an AngularJS run method. While I do not know the details of your backend implementation, surely you can return a variable which represents this information. You can simply craft something like the following...

如果您在后端拥有此知识,为什么不创建一个可以在AngularJS运行方法中调用的端点。虽然我不知道后端实现的细节,但您肯定可以返回表示此信息的变量。你可以简单地制作如下的东西......

app.run(['$rootScope', '$http', function ($rootScope, $http) {
    $http.get('/yourEndoint').success(function(response) {
        $rootScope.whereAmI = response.whereAmI;
    });
}]);

Where wereAmI is some value you return from your backend and return to this call. If you place it on $rootScope, all other $scope's will inherit this value so you'll have knoweldege of "where you're at" app wide.

其中areAmI是你从后端返回的一些值并返回此调用。如果你把它放在$ rootScope上,那么所有其他$ scope都将继承这个值,这样你就可以知道“你在哪里”app app。

For more info on the run function, check out the AngularJS module docs

有关run函数的更多信息,请查看AngularJS模块文档

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the services have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

运行块是Angular中与main方法最接近的东西。运行块是需要运行以启动应用程序的代码。在配置完所有服务并创建注入器后执行。运行块通常包含难以进行单元测试的代码,因此应在隔离模块中声明,以便在单元测试中忽略它们。

#2


1  

In my case, I use gulp/grunt build task to select the requested config.js file (ie. production.config.js). Then you can use something like npm args to set an --env=production or --env=development variable when running your build task, which then is used by the gulp/grunt task to grab the requested config file.

在我的例子中,我使用gulp / grunt构建任务来选择所请求的config.js文件(即.production.config.js)。然后,您可以使用npm args之类的东西在运行构建任务时设置--env = production或--env =开发变量,然后gulp / grunt任务使用它来获取请求的配置文件。

Then your actual config file will just be an angular.constant(...) component with settings to use for your app.

然后你的实际配置文件将只是一个angular.constant(...)组件,其中包含用于你的应用程序的设置。