2015-05-23 68 views
0

我定义了以下app.js:离子/科尔多瓦:模块无法找到

angular.module('myApp', ['ionic', 'myApp.controllers']) 

.run(function ($ionicPlatform) { 
    $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(); 
     } 
    }); 
}) 

.config(function ($stateProvider, $urlRouterProvider) { 
    $stateProvider.state('index', { 
     url: '/', 
     templateUrl: 'templates/home.html' 
    }).state('prints', { 
     url: '/prints', 
     templateUrl: 'templates/photo_prints.html' 
    }).state('box', { 
     url: '/box', 
     templateUrl: 'templates/photo_box.html' 
    }).state('book', { 
     url: '/book', 
     templateUrl: 'templates/photo_book.html' 
    }).state('framed', { 
     url: '/framed', 
     templateUrl: 'templates/photo_framed.html' 
    }); 

    $urlRouterProvider.otherwise("/"); 
}); 

这controllers.js:

angular.module('myApp.controllers').controller('HomeController', ['$scope', 'productsFactory', homeController]); 

function homeController($scope, productsFactory) { 
    $scope.products = []; 

    init(); 

    function init() { 
     $scope.products = productsFactory.getProducts(); 
    } 
} 

我也在我的索引添加两个脚本。 HTML:

<head> 
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *"> 
    <meta name="format-detection" content="telephone=no"> 
    <meta name="msapplication-tap-highlight" content="no"> 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> 

    <!-- Ionic --> 
    <link rel="stylesheet" type="text/css" href="lib/ionic/css/ionic.css"> 
    <script type="text/javascript" src="lib/ionic/js/ionic.bundle.js"></script> 

    <!-- myApp --> 
    <link rel="stylesheet" type="text/css" href="css/style.css"> 
    <script type="text/javascript" src="js/app.js"></script> 
    <script type="text/javascript" src="js/factory.js"></script> 
    <script type="text/javascript" src="js/controllers.js"></script> 
</head> 

但是当我尝试运行此我得到的错误:

[$injector:nomod] Module 'myApp.controllers' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 

我的控制器无法找到......为什么?我实现它完全像来自cordova或离子提供的标准模板。

回答

1

你应该定义你的控制器模块是这样的:

angular.module('myApp.controllers', []) 

所以......

angular.module('myApp.controllers', []) 
     .controller('HomeController', homeController); 

homeController.$inject = ['$scope', 'productsFactory']; 

function homeController($scope, productsFactory) { 
     $scope.products = []; 

    init(); 

    function init() { 
      $scope.products = productsFactory.getProducts(); 
    } 
} 

这是reference

UPDATE:

当你创建一个新的项目离子 - 让我们说sidemenu - 你将有一个文件controllers.js这里所有的控制器进行定义。

这就是你可以定义每一个控制您的应用程序将要使用的地方:

angular.module('myApp.controllers', []) 

.controller('sample1Controller', function($scope) { 

}) 

.controller('sample2Controller', function($scope) { 

}) 

.controller('sample3Controller', function($scope) { 

}); 

这里,在此示例中,我定义了3个控制器:sample1Controller,sample2Controller,sample3Controller。

这不是做的事情,但最好的方式。最佳做法是为每个控制器提供一个js文件。 现在我们说它够好了。

你的主要应用对myApp在这里定义:

angular.module('myApp', ['ionic', 'myApp.controllers']) 

,我想它会附着在你的HTML一些元素:

<body ng-app="myApp"> 
    <ion-nav-view></ion-nav-view> 
</body> 

你在你的应用程序一个注射几个模块:ionic和您的myApp.controllers

myApp.controllers是一个自定义模块:控制器的容器。

你必须从某个地方定义它。这是你会做的方式:

angular.module('myApp.controllers', []) 

如果你想你的控制器中的每个单独的文件中,你可以做这样的事情。 在一个文件中,你可以把你的控制器模块定义(app.controllers。JS):

angular.module('app.controllers', []); 

和eache文件,你可以有你自己的控制器:

home.js

angular.module('app.controllers') 
    .controller('homeController', function($scope) { 

}); 

contacts.js

angular.module('app.controllers') 
    .controller('contactsController', function($scope) { 

}); 

和当然,你有将每个控制器的js文件包含在您的index.html中。

你可以看看这个plunker

+0

也许这是一个更好的做法,但它并没有解决问题。 – Mulgard

+0

创建一个plunker,以便我们可以尝试修复它。 – LeftyX

+0

不幸的是我不知道你的意思。我使用'离子启动myApp sidemenu'创建了另一个项目,并使用它。但在我添加了另一个模块后,我遇到了同样的问题。它无法找到。 – Mulgard

0

只应添加您controllers.js顶部这一行:

angular.module('myApp.controllers', []);