您可以使用角依赖注入,而不是RequireJS?

编程入门 行业动态 更新时间:2024-10-24 15:19:50
本文介绍了您可以使用角依赖注入,而不是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文件(docs.angularjs/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解释,你可以添加<脚本方式> 标签到你的页面为每个文件

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

更多推荐

您可以使用角依赖注入,而不是RequireJS?

本文发布于:2023-10-10 22:39:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:您可以   而不是   RequireJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!