我想使用可以在項目中的任何位置調用的常量變量。不能將常量注入其他模塊angularjs ionic
我做了'constants.js'。
angular.module('myApp.constants', [])
.constant('const', (function(){
return {
username = 'abc'
}
})()
);
'app.js'
angular.module('myApp', ['ionic', 'myApp.controllers', 'myApp.constants'])
.run(function($ionicPlatform, $rootScope) {
$ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
});
})
'controllers.js'
angular.module('myApp.controllers', [])
.controller('LoginCtrl', function($scope, $ionicPopup, $http, $state, const) {
$scope.user_name = const.username;
});
當我打電話從控制器 '常量',我得到這個錯誤。
D/SystemWebChromeClient: file:///android_asset/www/js/controllers.js: Line 8 : Uncaught SyntaxError: Unexpected token const
I/chromium: [INFO:CONSOLE(8)] "Uncaught SyntaxError: Unexpected token const", source: file:///android_asset/www/js/controllers.js
我不熟悉angularjs及其模塊。 我希望有人能把我送到正確的方向。
在此先感謝。
它不工作:( – Thwe