我无法从服务中的 myConstant
检索 string
。
我在这里做错了什么以及如何解决?请提供代码示例。
// Define Angular App
var app = angular.module('App', ['ngRoute', 'LocalStorageModule', 'angular-loading-bar']).constant('myConstant', {
string: 'myString',
size: 30
});
'use strict';
app.factory('devicesService', ['$http', 'myConstant', function ($http) {
var serviceBase = myConstant.string; // PROBLEM HERE
...
最佳答案
这是在 Angular 中使用常量的示例:将其添加到 app.js
.constant('AUTH_EVENTS', {
AuthenticatedSuccess: 'auth-user-success',
})
在你的 Controller 中:
.controller('ApplicationController', function ($scope, AUTH_EVENTS) {
console.log(AUTH_EVENTS.AuthenticatedSuccess);
}
关于javascript - 如何在 Angular 中使用常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24733438/