问题
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