angularjs - 如何在 Angularjs 模板中访问常量

标签 angularjs global-variables constants

如何将普通js文件中定义的常量访问到不同模块的模板中。

如果我在我的 MainModule.js 中定义了一个像这样的常量,它包含在主 html 文件的开头

> var myApp = angular.module('AC' .....);
> myApp.constant('timeoutValue',5);

通过将“timeoutValue”传递给它,我能够访问我的 controller.js 中的常量。 但是我无法在我的 template.html/partial 中访问它。

如何在我的 template.html/partial 文件中访问“timeoutValue”

最佳答案

将它注入(inject)到您的 Controller 中并为其设置一个作用域变量。

app.constant('timeoutValue', 5);

app.controller('someController', function($scope, timeoutValue) {
  $scope.timeoutValue = timeoutValue;
});

关于angularjs - 如何在 Angularjs 模板中访问常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24338261/

相关文章:

mysql - 在使用 MySql、express、angular、nodejs 时,express 和 node 的端口是否不同

javascript - Angular js $scope.apply 函数不更新模板

javascript - Angular ng-repeat 显示对象项

javascript - 为什么 if(x) 与 if(window.x) 不同?

global-variables - Octave:清除全局变量后无法更改它的值

c++ - 为什么我的 C++ 程序不计算 'e' 的更多位数?

c++ - 如果我返回一个const定义的对象,为什么const关键字不合格?

从 const int* 到 int* 的 C++ 转换处理意外结果

javascript - Protractor 页面对象继承

Python加密函数和全局变量