angularjs - 如何在一个文件中定义常量并在angularjs中访问另一个文件

标签 angularjs node.js

我在我的应用程序中处理许多变量,我不想每次都定义字符串。相反,我想从一个文件访问字符串并访问我应用程序中的所有文件。

例如:

一个=1;

我想在我的应用程序中使用变量名 one 访问 1,因为将来它可以更改为 one(one = one),那时我不能在所有地方都更改。

为了避免这样的困难,我选择常量文件。

最佳答案

您可以创建服务并通过注入(inject)在任何 Controller 中访问它。

angular.module('app')
.constant('CONSTANTS', {something: 12});

你的 Controller

angular.module('app').controller('mainCtrl', function (CONSTANTS) {              
    $scope.mainctrlvariable = CONSTANTS.something;
});

你的另一个 Controller

angular.module('app').controller('otherCtrl', function (CONSTANTS) {              
        $scope.otherCtrlvariable = CONSTANTS.something;
    });

关于angularjs - 如何在一个文件中定义常量并在angularjs中访问另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37179349/

相关文章:

javascript - AngularJS - 如何在我的 Controller 中使用通过 Factory GET 请求检索到的数据?

javascript - 无法在 angularJS 中为 $http.get 添加自定义 header

javascript - 如何: Refresh AngularJS Route

node.js - 如何在node.js中将图像从请求传输到pdfkit?

mysql - 如何获取 nodejs mysql 连接池中未使用/已使用的连接数?

javascript - 使用 Angular 从 JSON 调用单个结果

javascript - Ui路由器刷新问题

javascript - "FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory"与 npm 搜索

javascript - Node.js 错误 : Object function has no method

node.js - 如何使用 webpack 处理 ejs View