2015-03-25 37 views
4

我已經進入了一個問題,我的AngularJS應用程序的邏輯分割成多個文件給我一個問題,只有最後一個工作,並可能跳過以前的問題。AngularJS分成多個文件 - 只有最後一個工作

這裏的index.html的包容:

<script src="js/app.js"></script> 
<script src="js/app.config.js"></script> 
<script src="js/controllers/HomeController.js"></script> 

每個文件都含有非常少量的邏輯:

初始化:

angular.module('sportcial', ['ionic']) 
.run(function run($ionicPlatform) { 
    alert(2); 
    $ionicPlatform.ready(function() { 
     // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
     // for form inputs) 
     if(window.cordova && window.cordova.plugins.Keyboard) { 
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     } 

     if(window.StatusBar) { 
      StatusBar.styleDefault(); 
     } 
    }); 
}); 

配置:

angular.module('sportcial', ['ionic']).config(function configApp($ionicConfigProvider) { 
    alert(1); 
    $ionicConfigProvider.tabs.position('bottom'); 
}); 

HomeContr奧勒:

angular.module('sportcial', ['ionic']) 
    .config(function config($stateProvider, $urlRouterProvider) { 
     alert(3) 
    }) 
    .controller('HomeController', ['$scope', function HomeController($scope) { 
     var home = this; 
    }] 
); 

,只有最後一個觸發警報(3)...

缺少什麼我在這裏的傢伙? :)

回答

6

您正在覆蓋每個文件中的sportcial模塊。

使用的setter語法

angular.module("moduleName", [moduleDependencies, ..]) 

要創建模塊。

如果你想要的東西添加到現有的模塊,你需要調用的getter:

angular.module("moduleName") 

因此,例如,你需要更新你的配置文件:

angular.module('sportcial').config(function configApp($ionicConfigProvider) { 
    alert(1); 
    $ionicConfigProvider.tabs.position('bottom'); 
}); 
1

任何angular module只能定義爲一次及其相關性:

angular.module('sportcial', ['ionic']) 

但它可以通過調用module而不依賴檢索需要多次:

angular.module('sportcial').config(.... 
angular.module('sportcial').controller(.... 

你必須確保包括與依賴模塊定義之前,包括所有其他文件一樣模塊。

'Creation versus Retrieval'參見:

要注意的是使用angular.module('myModule', [])將創建 模塊myModule並覆蓋命名myModule任何現有的模塊。使用 angular.module('myModule')檢索現有模塊

1

您正在每個文件中聲明一個新模塊。

用於設定模塊角語法是:

angular.module('moduleName', dependenciesArray); 

用於獲取模塊,並隨後確定它是模塊的語法如下:

angular.module('moduleName'); 

與從離子依賴性取出數組的配置和控制器文件。

相關問題