简体   繁体   English

如何要求角度控制器

[英]How to Require Angular Controllers

Im new to Angularjs and i am trying to integrate with my application that uses RequireJS. 我是Angularjs的新手,我正尝试与使用RequireJS的应用程序集成。 I have the application working on a test page using the ng-submit . 我使用ng-submit在测试页上运行应用程序。 However, in my app.js file i dont think i am "require"ing my controllers in the best way. 但是,在我的app.js文件中,我认为我并不是以最佳方式“要求”我的控制器。

I am using AngularJS v1.1.5 我正在使用AngularJS v1.1.5

Here's my tree: 这是我的树:

resources
   - CSS
   - js
        - controllers
             - TestController1.js
             - TestController2.js
             - TestController3.js
             - TestController4.js
             - TestController5.js
        - libs
             - angular.js
             - jquery.js
             - require.js
             - mondernizr.js
             ......
             ......
             ......
        main.js
        app.js
pages
        test1.html
        test2.html
        test3.html
        test4.html
        test5.html

main.js main.js

(function(require) {
'use strict';

require.config({
    baseUrl: '/libs',
    paths: {
        'zepto'     : 'zepto',
        'jquery'    : 'jquery',
        'angular'   : 'angular',
        'router'    : 'page',
        'history'   : 'history.iegte8',
        'event'     : 'eventemitter2'
    },
    shim: {
        'zepto'     : { exports: '$' },         
        'angular'   : { deps: ['jquery'], exports: 'angular' },
        'app'       : { deps: ['angular'] },
        'router'    : { exports: 'page'},
        'modernizr' : { exports: 'Modernizr' }
    }
});

require(['angular', 'app'], function(angular) {
    'use strict';
    angular.bootstrap(document, ['app']);
}); 

})(this.require);

app.js app.js

define("app", ["angular"], function(angular){

    var app = angular.module("app", []);

    app.config(function($routeProvider, $locationProvider){
        $routeProvider
            .when("/test1", {
                templateUrl: "test1.html",
                controller: "TestController1"
            })
            .when("/test2", {
                templateUrl: "test2.html",
                controller: "TestController2"
            })
            .when("/test3", {
                templateUrl: "test3.html",
                controller: "TestController3"
            })
            .when("/test4", {
                templateUrl: "test4.html",
                controller: "TestController4"
            })
            .when("/test5", {
                templateUrl: "test5.html",
                controller: "TestController5"
            })
            .otherwise({ redirectTo: '/test1'});            
    });

    return app;
});

require(["app", "controllers/TestController1"]);
require(["app", "controllers/TestController2"]);
require(["app", "controllers/TestController3"]);
require(["app", "controllers/TestController4"]);
require(["app", "controllers/TestController5"]);

TestController1-5.js TestController1-5.js

require(['app'], function(app) {
    app.controller("TestController1", function($scope) {        
        $scope.clickMe = function()  {                      
            alert('TestController1.js is responding');
            $scope.title = "Title";
            $scope.data = {message: "Hello"};
            $scope.message = "Hello World!";            
        };
    });
 });

test1-5.html test1-5.html

<div ng-controller="TestController1">       

    <form ng-submit="clickMe()">
        <div>
            <button type="submit">Test TestController1</button>
        </div>
    </form>

    {{ data.message + " world" }}
    {{ title }}    

</div>

Equally if you think there are other ways i can improve my code and code structure feel free to suggest. 同样,如果您认为还有其他方法可以改善我的代码,并且可以随意建议代码结构。

Thanks 谢谢

Maybe you could just improve your code by making an "app.controllers" module that will be in charge of loading all your controllers. 也许您可以通过制作一个“ app.controllers”模块来改善代码,该模块将负责加载所有控制器。 Then in your app.js, you just add this module as dependency. 然后在app.js中,您只需将此模块添加为依赖项即可。

So for instance. 例如。

app/controllers/MyController.js: 应用程序/控制器/ MyController.js:

define(['angular'], function(angular) {
  return angular.module('app.controllers.MyCtrl', [])
    .controller('MyCtrl', [function(){
      [...]
    }]);

app/controllers.js: 应用程序/ controllers.js:

define([
  'angular',
  'app/controllers/MyController'
],
function(angular) {
  return angular.module('app.controllers', [
    'app.controllers.MyCtrl'
  ]);
});

app/app.js: 应用程序/ app.js:

define("app", "app/controllers", ["angular"], function(angular){

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

  app.config(function($routeProvider, $locationProvider){
    $routeProvider
      .when("/my", {
         templateUrl: "my.html",
         controller: "MyCtrl"
      })
      .otherwise({ redirectTo: '/test1'});            
  });

  return app;
});

You could also load controllers asynchronously this way for instance: 您还可以通过这种方式异步加载控制器,例如:

angular.module('myApp.controllers', [])
  .controller('MyCtrl', ['$scope','$injector', function($scope, $injector){
    require(['app/controllers/MyController'], function(MyCtrl){
      $injector.invoke(MyCtrl, this,{'$scope':$scope});
    });
  }]);

The downpoint of this kind of loading is that you will have to manually call $scope.$apply(); 这种加载的缺点是您必须手动调用$ scope。$ apply();。 in order to manually ask for a digest otherwise your scope modifications on this controller will not be taken into account as it is with "standard" loading. 为了手动请求摘要,否则将不考虑在此控制器上进行的范围修改,因为在“标准”加载中是一样的。 However, I will not recommend this kind of loading. 但是,我不建议这种加载。 In the end when the code is minified and optimized in one file with r.js, it does not make much sense to "lazy load" code. 最后,当使用r.js在一个文件中最小化和优化代码时,“延迟加载”代码没有多大意义。

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

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