javascript - 调用模块常量?

标签 javascript angularjs

我有以下 Angular 模块。我如何从我的 Controller 之一调用 APIHost?

angular.module('configuration', [])
    .constant('APIHost','http://api.com')
    .constant('HostUrl','http://example.com')
    .constant('SolutionName', 'MySite');

最佳答案

Constant只不过是一种提供者配方。

您需要在controller工厂函数中注入(inject)constant依赖项,就是这样。

app.controller('testCtrl', function($scope, APIHost){
  console.log(APIHost)
})

Make sure your configuration module has been added to main module as dependency to get use of constant's provider like below

var app = angular.module('app', ['configuration', 'otherdependency']);
app.controller( ... ) //here you can have configuration constant available

关于javascript - 调用模块常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35752464/

相关文章:

javascript - AngularJS 服务获取数据,但不将其返回给 Controller ?

javascript - MongoDB/Mongoose - 仅当特定字段唯一时才将对象添加到对象数组

javascript - AngularJS 的堆栈跟踪?

javascript - 根据 Vue 文档计算嵌套 v-for 循环内的 vs 方法

javascript - 对输入值求和并在表单中选择

javascript - datetimepicker - 将日期值传递给 Angular Controller

javascript - Angularjs:如何编写 $http 作为 Controller 的 promise

angularjs - 在 angularjs 的文本框中显示建议

javascript - 在 ng-click AngularJS 上动态更改 div 内容

Javascript 按年份 DESC、月份 ASC、日期 ASC 对数组中的日期进行排序