Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t向其他模块注入常数angularjs_Angularjs_Ionic Framework_Global Variables_Constants_Angular Module - Fatal编程技术网

Can';t向其他模块注入常数angularjs

Can';t向其他模块注入常数angularjs,angularjs,ionic-framework,global-variables,constants,angular-module,Angularjs,Ionic Framework,Global Variables,Constants,Angular Module,我想使用常量变量,它可以从项目中的任何地方调用 我制作了“constants.js” angular.module('myApp.constants', []) .constant('const', (function(){ return { username = 'abc' } })() ); “app.js” angular.module('myApp', ['ionic', 'myApp.controllers', 'myApp.constants']) .run(

我想使用常量变量,它可以从项目中的任何地方调用

我制作了“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;
});
当我从控制器调用“const”时,我得到了这个错误

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及其模块。 我希望有人能把我送到正确的方向


提前谢谢。

试着像这样声明常数

angular.module('myApp.constants', []).constant('const', {username: 'abc'});
angular.module('myApp.constants',[])
.constant('constantMessage',(function(){
返回{
用户名:“abc”
}
})());
angular.module('myApp',['myApp.constants']))
.controller('LoginCtrl',函数($scope,constantMessage){
$scope.user_name=constantMessage.username;
});

{{user_name}}

我想这和我在常量中写的是一样的。js请将名称“const”更改为任何其他名称,因为angular将“const”作为默认关键字,并使用“:”而不是“=”我将名称更改为“CommonConstants”,我得到了这个错误。“ReferenceError:CommonConstants未在上述代码中定义”生成“constant.js”文件并包含在index.html中