How to access constants in Angularjs Template

别说谁变了你拦得住时间么 提交于 2019-12-12 12:32:44

问题


How do I access constants defined in common js file into template of different module.

if I have defined a constant like this in my MainModule.js which is included in the beginning of the main html file

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

I was able to access my constant in my controller.js by passing "timeoutValue" to it. But I am unable to access it in my template.html/partial.

How can I access "timeoutValue" in my template.html/partial file


回答1:


Inject it into your controller and set a scope variable to it.

app.constant('timeoutValue', 5);

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



回答2:


Try to assign constant to $rootScope and use in template.

myModule.constant('brand', 'MyBrand');
myModule.run(['$rootScope','brand', function ($rootScope,brand) {
    $rootScope.brand = brand;
}]);    


来源:https://stackoverflow.com/questions/24338261/how-to-access-constants-in-angularjs-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!