简体   繁体   English

如何拥有多个控制器文件?

[英]How can I have multiple controller files?

SOLVED 解决了

So after viewing your responses and making minor changes to my code, I found a simple typo which prevented me to reach the result I was after. 因此,在查看了您的答复并对我的代码进行了微小的更改之后,我发现了一个简单的错字,使我无法达到想要的结果。 So thank you all to helping with where I was going wrong, I've now got the controllers seperated and everything's working as planned! 因此,感谢大家提供帮助以解决我要去的地方,现在我把控制器分开了,一切都按计划进行了!

---- ----

I am currently developing a hybrid mobile application using Cordova, Ionic and AngularJS within Visual Studio 2015. Due to the vast amount of code I have in my single controller.js file, I want to separate the code so I have a .js controller file per template; 我目前正在Visual Studio 2015中使用Cordova,Ionic和AngularJS开发混合移动应用程序。由于我的单个controller.js文件中包含大量代码,因此我想将代码分开,所以我有一个.js控制器文件每个模板; instead of everything in one file. 而不是将所有内容都放在一个文件中。 Unfortunately, II do not understand how to pull this off (learning AngularJS still). 不幸的是,II不了解如何实现这一目标(仍在学习AngularJS)。 I've done some researched but the majority of examples I have seen show a very simple demo, which I replicate with my own code but it still doesn't work. 我已经进行了一些研究,但是我看到的大多数示例都显示了一个非常简单的演示,我使用自己的代码进行了复制,但仍然无法正常工作。 So I was hoping if someone can give me an insight where I may be going wrong. 因此,我希望有人能给我一个见解,指出我可能会出错。

File Structure in /www / www中的文件结构

  • index.html index.html

/js / js

  • app.js app.js

  • controllers.js controllers.js

/js/controllers / js /控制器

  • login.js login.js

  • sales.js sales.js

/templates /模板

  • login.html login.html

  • sales.html sales.html

/js/app .js / js / app .js

angular.module('main', ['ionic', 'main.controllers', 'chart.js', 'ngCordova', 'ngIOS9UIWebViewPatch', 'angular.filter'])

    .config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
        $stateProvider

        .state('login', {
            cache: false,
            url: "/login",
            templateUrl: "templates/login.html",
            controller: "LoginCtrl"
        })

        .state('sales', {
            cache: false,
            url: "/sales",
            templateUrl: "templates/sales.html",
            controller: "SalesCtrl"
        })

        $urlRouterProvider.otherwise('/login')
        $ionicConfigProvider.views.swipeBackEnabled(false);
    });

/js/controllers.js /js/controllers.js

angular.module('main.controllers', ['ionic', 'ngCordova']);

/js/controllers/login.js /js/controllers/login.js

angular.module('main.controllers', [])
.controller("LoginCtrl", function ($scope, $state, $cordovaSQLite, $timeout, $ionicPopup, $cordovaDevice, $ionicLoading, $cordovaKeyboard, $cordovaToast) {
    $ionicLoading.show({
        template: 'Loading...'
    });
// DO STUFF

/js/controllers/sales/js / js / controllers / sales / js

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

.controller("SalesCtrl", function ($scope, $state, $http, $ionicLoading, $cordovaSQLite, $cordovaToast) {
    $ionicLoading.show({
        template: 'Loading data...'
    });
// DO STUFF

Following this structure, I get this error (quote below): https://docs.angularjs.org/error/ng/areq?p0=LoginCtrl&p1=not%20a%20function,%20got%20undefined 按照这种结构,我得到此错误(在下面引用): https : //docs.angularjs.org/error/ng/areq?p0=LoginCtrl&p1=not%20a%20function,%20got%20undefined

Argument 'LoginCtrl' is not a function, got undefined 参数'LoginCtrl'不是一个函数,未定义

I managed to get it to work slightly, only when I had login.js and not sales.js, but of course $state.* stopped working when trying to change template. 我设法使其稍作工作,仅当我有login.js而不是sales.js时,但是当尝试更改模板时,$ state。*当然停止工作了。 So I know that wasn't 100% either. 所以我知道那也不是100%。 Hopefully this will make sense, fi it doesn't just clarify what I may be not making sense in, and I shall explain more if need be, appreciate any help. 希望这将是有道理的,因为它不仅可以澄清我可能没有什么意义,而且我将在需要时解释更多,感谢您的帮助。 :) :)

EDIT 编辑

index.html index.html

<!-- App references -->
<link href="css/ionic.css" rel="stylesheet" />
<link href="css/angular-chart.css" rel="stylesheet" />
<link href="css/index.css" rel="stylesheet" />

<script src="lib/ionic/ionic.bundle.js"></script>
<script src="lib/ngCordova/ng-cordova.js"></script> <!-- Must be after Ionic but before Cordova-->
<script src="cordova.js"></script>
<script src="scripts/index.js"></script>

<script src="lib/angular-cookies/angular-cookies.min.js"></script>
<script src="lib/angular-chart/Chart.min.js"></script>
<script src="lib/angular-chart/angular-chart.min.js"></script>
<script src="lib/angular-ios9-uiwebview.patch.js"></script>
<script src="lib/angular-filter/angular-filter.min.js"></script>
<script src="js/directives/favourite.js"></script>

<script src="js/controllers.js"></script>
<script src="js/controllers/login.js"></script>
<script src="js/controllers/sales.js"></script>
<script src="js/app.js"></script>

You are redefining your module again & again in your each controller file. 您将在每个控制器文件中一次又一次地重新定义模块。 Which is clearing out old registered controller from that module. 这将从该模块中清除旧的注册控制器。

You have already defined that module in your /js/controllers.js . 您已经在/js/controllers.js定义了该模块。

angular.module('main.controllers', ['ionic', 'ngCordova']);

So reuse that module in other Javascript files when your are binding any component to it as below. 因此,当您将任何组件绑定到该模块时,请在其他Javascript文件中重用该模块,如下所示。

angular.module('main.controllers')

The best approach is that you clearly separate module creation from module usage. 最好的方法是将模块创建与模块用法明确分开。

modules.js: modules.js:

angular.module('main', ['main.sales']);
angular.module('main.sales', []);

src/sales/scripts/sales-controller.js: src / sales / scripts / sales-controller.js:

angular.module('main.sales').controller(function() {});

If you concat and minify your js files via grunt or gulp, you should always explicitly include modules.js first, afterwards you can include the rest via a pattern like 'src/**/*.js' for example. 如果通过grunt或gulp合并并缩小js文件,则应始终始终首先显式包含modules.js,然后再通过类似src / ** / *。js的模式包含其余部分。

This way the modules are always defined before they are used. 这样,始终在使用模块之前对其进行定义。 If that's not the case, angular will complain about a non existing module. 如果不是这种情况,Angular会抱怨不存在的模块。

PS: it's way better to create functional modules (sales related functionality in 1 module) instead of technical modules (all controllers in 1 module) PS:最好是创建功能模块(1个模块中与销售相关的功能),而不是技术模块(1个模块中的所有控制器)

You are declaring the main.controllers module twice, once for each controller. 您要声明main.controllers模块两次,每个控制器一次。 Also, it's not strictly required to declare a separate module for the controllers, you could just declare the controllers within your main module. 另外,并非严格要求为控制器声明一个单独的模块,您只需在主模块中声明控制器即可。 Some would argue that you lose reusability - and they would be right - but depending on the size of your project and how tightly coupled your controllers are with your application (90% of the time the answer is: very ) you could go that way. 有人会说您失去了可重用性-这是正确的-但是取决于项目的大小以及控制器与应用程序的紧密耦合程度(90%的答案是: 非常 ),您可以采用这种方式。 Since you are probably just starting out, try doing something like this: 由于您可能刚开始,请尝试执行以下操作:

js/app.js js / app.js

angular.module('main', ['ionic', 'chart.js', 'ngCordova', 'ngIOS9UIWebViewPatch', 'angular.filter']) angular.module('main',['ionic','chart.js','ngCordova','ngIOS9UIWebViewPatch','angular.filter'])

.config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
    $stateProvider

    .state('login', {
        cache: false,
        url: "/login",
        templateUrl: "templates/login.html",
        controller: "LoginCtrl"
    })

    .state('sales', {
        cache: false,
        url: "/sales",
        templateUrl: "templates/sales.html",
        controller: "SalesCtrl"
    })

    $urlRouterProvider.otherwise('/login')
    $ionicConfigProvider.views.swipeBackEnabled(false);
});

Note that I am no longer depending on main.controllers ? 请注意,我不再依赖main.controllers吗? That's because next I'm doing this: 那是因为接下来我要这样做:

angular.module('main')
.controller("LoginCtrl", function ($scope, $state, $cordovaSQLite, $timeout,       $ionicPopup, $cordovaDevice, $ionicLoading, $cordovaKeyboard, $cordovaToast) {
$ionicLoading.show({
    template: 'Loading...'
});
// DO STUFF

And this: 和这个:

angular.module('main')
.controller("SalesCtrl", function ($scope, $state, $http, $ionicLoading, $cordovaSQLite, $cordovaToast) {
$ionicLoading.show({
    template: 'Loading data...'
});
// DO STUFF

The different controllers can (and should) be declared each in a separate file, so that you have a clear structure. 可以(并且应该)在不同的文件中分别声明不同的控制器,以便您拥有清晰的结构。 It might be more correct to have a separate module for the controllers, and I fear my opinion might be unpopular, but I don't really see the point - whereas I strongly urge you to separate your services and your directives into different modules, since it's much more likely that you're going to use them again in other projects. 为控制器使用单独的模块可能更正确,并且我担心我的意见可能不受欢迎,但我并没有真正理解要点-但我强烈建议您将服务和指令分成不同的模块,因为您很有可能会在其他项目中再次使用它们。

My suggest we will organize code by split to the module, and inject it in the app.js file 我建议我们将代码拆分为模块来组织代码,并将其注入到app.js文件中

This is detail my way: https://mymai91.github.io/ionic/2016/07/01/ionic-structure-code-for-project.html 这是我的详细信息: https : //mymai91.github.io/ionic/2016/07/01/ionic-structure-code-for-project.html

Code demo: https://github.com/mymai91/mymaiApp 代码演示: https : //github.com/mymai91/mymaiApp

to anyone looking at the "solved code"... The Square brackets in the inner controlleres should be removed: 给任何看“已解决的代码”的人...内部控制器中的方括号应删除:

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

--> ->

angular.module('main.controllers')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM