重新打开,并添加依赖关系已经自举程序 [英] re-open and add dependencies to an already bootstrapped application

查看:154
本文介绍了重新打开,并添加依赖关系已经自举程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法注入一晚依赖于一个已经自举角模块?这就是我的意思是:

Is there a way to inject a late dependency to an already bootstrapped angular module? Here's what I mean:

说,我有一个站点范围内的应用角度,定义为:

Say that I have a site-wide angular app, defined as:

// in app.js
var App = angular.module("App", []);

而在每一页:

<html ng-app="App">

后来,我重新打开应用程序基于当前页面的需要添加逻辑:

Later on, I'm reopening the app to add logic based on the needs of the current page:

// in reports.js
var App = angular.module("App")
App.controller("ReportsController", ['$scope', function($scope) {
  // .. reports controller code
}])

现在,说是逻辑的那些点播位之一也要求自己的依赖关系(如 ngTouch ngAnimate ngResource 等)。我怎么能把他们基本应用程序?这似乎不工作:

Now, say that one of those on-demand bits of logic also requires their own dependencies (like ngTouch, ngAnimate, ngResource, etc). How can I attach them to the base App? This doesn't seem to work:

// in reports.js
var App = angular.module("App", ['ui.event', 'ngResource']); // <-- raise error when App was already bootstrapped

我知道我可以做事先做好准备,即 -

I realize I can do everything in advance, i.e -

// in app.js
var App = angular.module("App", ['ui.event', 'ngResource', 'ngAnimate', ...]);

或定义自身的每一个模块,然后注入到所有主要的应用程序(<一个href=\"http://henriquat.re/modularizing-angularjs/modularizing-angular-applications/modularizing-angular-applications.html\">see这里更多):

Or define every module on its own and then inject everything into the main app (see here for more):

// in reports.js
angular.module("Reports", ['ui.event', 'ngResource'])
.controller("ReportsController", ['$scope', function($scope) {
  // .. reports controller code
}])

// in home.js
angular.module("Home", ['ngAnimate'])
.controller("HomeController", ['$scope', '$http', function($scope, $http){
  // ...
}])

// in app.js, loaded last into the page (different for every page that varies in dependencies)
var App = angular.module("App", ['Reports', 'Home'])

但这就需要我初始化应用程序每次与当前页面的相关性。

But this will require I initialize the App everytime with the current page's dependencies.

我preFER包括基本在每一个页面,并简单介绍一下所需的扩展到每个页面( reports.js app.js home.js 等),而无需修改引导逻辑每次我添加或删除一些东西。

I prefer to include the basic app.js in every page and simply introduce the required extensions to each page (reports.js, home.js, etc), without having to revise the bootstrapping logic everytime I add or remove something.

有没有办法引进的依赖,当应用程序已经自举?什么被认为是地道的方式(或方法)来做到这一点?我倾向于后者的解决方案,但想看看,如果我所描述的方法也可以这样做。谢谢。

Is there a way to introduce dependencies when the App is already bootstrapped? What is considered the idiomatic way (or ways) to do this? I'm leaning towards the latter solution, but wanted to see if the way I described could also be done. thanks.

推荐答案

我解决了它是这样的:

再次引用应用程序:

var app = angular.module('app');

然后把你的新要求,要求数组:

then push your new requirements to the requirements array:

app.requires.push('newDependency');

这篇关于重新打开,并添加依赖关系已经自举程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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