gpt4 book ai didi

javascript - 如何将值从 Node/快速应用程序传递到 AngularJS Controller

转载 作者:行者123 更新时间:2023-11-30 16:50:12 25 4
gpt4 key购买 nike

我正在开发一个基于 mean.js 样板的应用。

在我的一个 Angular Controller 中,我需要“知道”应用程序是在开发、测试还是生产环境中运行。

当托管 Node 应用程序启动时,这是通过 NODE_ENV 环境变量指示的,因此 Node 知道它在哪里运行。

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

最佳答案

如果您对后端有这方面的了解,为什么不创建一个可以在 AngularJS run 方法中调用的端点。虽然我不知道你的后端实现的细节,但你肯定可以返回一个代表此信息的变量。您可以简单地制作如下内容...

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

wereAmI 是您从后端返回并返回到此调用的某个值。如果您将它放在 $rootScope 上,所有其他 $scope 将继承此值,因此您将了解应用范围内的“您所在的位置”。

有关 run 函数的更多信息,请查看 AngularJS module docs

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.

关于javascript - 如何将值从 Node/快速应用程序传递到 AngularJS Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30632366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com