您可以使用角依赖注入,而不是RequireJS? [英] Can you use Angular dependency injection instead of RequireJS?

查看:99
本文介绍了您可以使用角依赖注入,而不是RequireJS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用角,我怎么会分手alll从一个应用程序中的code到许多文件?我看了60ish分钟介绍,他们提到,我能做到这一点,而不requirejs或任何其他框架。

I'm starting with angular, how could I break alll the code from one app into many files?, I watched the 60ish minutes intro, and they mentioned that I could do this without requirejs or any other framework.

可以说我有这样的事情是工作得很好:

Lets say I have something like this that works just fine:

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

app.factory('ExampleFactory', function () {
   var factory = {};
   factory.something = function(){
   /*some code*/
   }
   return factory;
});


app.controller ('ExampleCtrl', function($scope, ExampleFactory){
   $scope.something = function(){
      ExampleFactory.something();
    };
});

app.config(function ($routeProvider) {
    $routeProvider
    .when('/',
    {
    controller: 'ExampleCtrl',
        templateUrl: 'views/ExampleView.html'
    })
    .otherwise({ redirectTo: '/' });
});

如果我想有它在单独的文件?像这样

What if I wanted to have it in separate files? like this

文件中的一个:

   angular.module('factoryOne', [])
   .factory('ExampleFactory', function () {
       var factory = {};
       factory.something = function(){
       /*some code*/
       }
       return factory;
   });

文件中的两个:

  angular.module('controllerOne', ['factoryOne'])
  .controller ('ExampleCtrl', function($scope,ExampleFactory){
      $scope.something = function(){
      ExampleFactory.something();
      };
  });

文件中的三个:

angular.module('routes', ['controllerOne'])
.config(function ($routeProvider) {
     $routeProvider
     .when('/',
     {
    controller: 'ExampleCtrl',
        templateUrl: 'views/ExampleView.html'
     })
    .otherwise({ redirectTo: '/' });
 });

文件之四:

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

我试过像这样,它不工作。
我可以做这样的事情,只是有在主视图文件四script标签?还是我必须有每个文件一个脚本标记?
感谢您的帮助球员。

I've tried it like this and it doesn't work. Can I do something like this and just have a script tag for File Four in the main view? or do I have to have one script tag per file?. Thanks for the help guys.

推荐答案

AngularJS目前还没有一个脚本加载器作为框架的一部分。为了加载依赖关系,你需要使用第三方装载机如RequireJS,的script.js等。

AngularJS does not currently have a script loader as part of the framework. In order to load dependencies, you will need to use a third party loader such as RequireJS, script.js, etc.

每Docs文件(<一个href=\"http://docs.angularjs.org/guide/module#asynchronousloading\">http://docs.angularjs.org/guide/module#asynchronousloading):

异步加载

模块是管理$喷射器的方式
  配置,并有无关的脚本加载到
  VM。有哪些处理脚本加载,现有的项目,其中
  可以与角一起使用。由于模块什么也不做在加载时,他们
  可以加载到虚拟机以任何顺序和由此脚本装载机可以
  利用这个特性的优势,并行加载过程。

Modules are a way of managing $injector configuration, and have nothing to do with loading of scripts into a VM. There are existing projects which deal with script loading, which may be used with Angular. Because modules do nothing at load time they can be loaded into the VM in any order and thus script loaders can take advantage of this property and parallelize the loading process.

...或者,作为@xanadont解释,你可以添加&LT;脚本方式&gt; 标签到你的页面为每个文件

...or, as @xanadont explained, you can add <script> tags to your page for every file.

这篇关于您可以使用角依赖注入,而不是RequireJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆