admin管理员组文章数量:1332383
Is it possible to access to a method of a factory service, before Angular has been bootstrapped, similar to the code below?
I need to make a number of AJAX requests before Angular starts in order to set a number of global application variables. I had hoped to keep the logic for this and/or store the responses inside an Angular service, and return a promise...
<script src="scripts/app.js"></script>
<script src="scripts/factories/app.js"></script>
<script>
angular.element(document).ready(function() {
factoryName.startup().then(function() {
angular.bootstrap(document, ['MyApp']);
}, function(err) {
console.log(error fetching bootstrap data);
}
});
</script>
Is there an alternative method to use to get a similar behaviour?
Is it possible to access to a method of a factory service, before Angular has been bootstrapped, similar to the code below?
I need to make a number of AJAX requests before Angular starts in order to set a number of global application variables. I had hoped to keep the logic for this and/or store the responses inside an Angular service, and return a promise...
<script src="scripts/app.js"></script>
<script src="scripts/factories/app.js"></script>
<script>
angular.element(document).ready(function() {
factoryName.startup().then(function() {
angular.bootstrap(document, ['MyApp']);
}, function(err) {
console.log(error fetching bootstrap data);
}
});
</script>
Is there an alternative method to use to get a similar behaviour?
Share Improve this question asked Nov 11, 2013 at 14:41 umpljazzumpljazz 1,2224 gold badges12 silver badges21 bronze badges2 Answers
Reset to default 4You can make the first service calls in module run blocks. When a later service call is made for those variables, you can either serve them out of $http's cache or have explicitly cached the promise from the first call.
// example
myApp.run(function(MyService) {
// call your function when Angular starts up
MyService.init();
});
Below is an example that loads a configuration file before bootstrapping the application.
The first call to bootstrap is done to gain access to angular services like $http and $location (you could also inject your own module at this point to access custom services).
After the configuration file has been loaded, angular.bootstrap is called for the main application, with the loaded configuration set as a constant on a makeshift module(rsacAppBootstrap) that is injected.
Here are at least two advantages over using a promise set from a run block:
- Reduced boilerplate of promise for everything dependent on configuration
- Ability to conditionally load dependencies based on the environment using RequireJS
Custom bootstrap script:
angular.bootstrap().invoke(function ($http, $location) {
var env = $location.search()._env || null;
if (env === true) {
env = null;
}
var configUri = 'config/env.json';
if (env) {
configUri = configUri.replace('json', env + '.json');
}
var rsacAppBootstrap = angular.module('rsacAppBootstrap', [])
.run(function ($rootScope, $location, $window) {
var env = $location.search()._env;
$rootScope.$on('$locationChangeSuccess', function () {
var newEnv = $location.search()._env;
if (env !== newEnv) {
$window.location.reload();
}
})
});
function bootstrap(config) {
rsacAppBootstrap.constant('rsacConfig', config || {});
angular.element(document).ready(function () {
var modules = ['rsacApp', 'rsacAppBootstrap'];
if (config.modules){
config.modules.forEach(function(v){
modules.push(v);
})
}
angular.bootstrap(document, modules);
});
}
$http.get(configUri)
.success(function (config) {
config._env = env;
if (config.require) {
require(config.require, function(){
bootstrap(config);
});
} else {
bootstrap(config);
}
})
.error(function () {
bootstrap();
});
});
Example configuration file:
{
"_meta": [
"Development environment settings"
],
"require":[
"//code.angularjs/1.2.3/angular-mocks.js",
"ponents/rsacMock/js/rsacMock.js"
],
"modules":[
"ngMockE2E",
"rsacMock"
],
"resources": { ... }
}
本文标签: javascriptAccess Angular service when manually bootstrappinginitializingStack Overflow
版权声明:本文标题:javascript - Access Angular service when manually bootstrappinginitializing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742304502a2449642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论