使用IIFE功能在角度js中的其他控制器中无法访问常量值

时间:2016-03-18 13:34:30

标签: javascript angularjs iife

以下是我的示例angular js代码,我已经声明了myApp模块,我想在两个不同的文件中编写代码, 在第一个文件中我已声明模块和一个常量文件,在其他文件中我已声明控制器。我想访问控制器中的CLientId的常量值,但它无法访问请提供解决方案

              

<div ng-app="myApp" ng-controller="myCtrl as vm">
{{ vm.firstName + " " + vm.lastName + " "+ vm.getName(); }}
</div>

<script>
// i want to diclare above code into different js files
//app.js 
(function(){

angular.module("myApp", []).value('clientId', 'a12345654321x');

})();

//con.js 
(function(){
angular.module("myApp", []).controller("myCtrl", function(clientId) {

    this.firstName = "John";
    this.lastName = "Doe";
    this.getName= name;
    function name()    {
        return  clientId ; 
     }
});

})();

</script>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

在重新定义[]模块

时删除myApp
(function(){

angular.module("myApp").value('clientId', 'a12345654321x');

})();

答案 1 :(得分:0)

控制器注册不应该有[]括号,因为它会覆盖已创建的模块。这样做:

(function(){
angular.module("myApp").controller("myCtrl", function(clientId) {

    this.firstName = "John";
    this.lastName = "Doe";
    this.getName= name;
    function name()    {
        return  clientId ; 
     }
});

})();