我正在学习angularjs和开发几个应用程序(同时,老板的命令)。我的所有应用程序都有一些与初始化有关的常见任务,但我不知道如何使这些任务成为可重用模块(是的,也许我是一个noob)。我研究了很多,但我只是更困惑。 :(angularjs应用程序引导:制作可重用模块
好了,这里是我需要可重用的角度模块的代码。我们的想法是,应用程序之前运行这些功能做任何事情。
/**
* INITIALIZATION - STEP 1 - This is the entry point
* @param {function} callback
* @returns {undefined}
*/
function runApplication(callback) {
showLoadingBar();
$.getJSON("myUrl").done(function (data) {
// do stuf, with error verification, then...
step2(callback, data);
}).fail(function() {
showCriticalErrorMessage("foo");
hideLoadingBar();
});
}
/**
* INITIALIZATION - STEP 2
* @param {function} callback
* @param {object} whateverData
* @returns {undefined}
*/
function step2(callback, whateverData) {
// do stuff with whateverData, with error verification, then...
$.ajax({
"url": "myOtherUrl",
"type": "GET",
"dataType": "json",
"contentType": "application/json; charset=utf-8"
}).done(function (data) {
// do stuff with data, with error verification, then...
step3(callback);
}).fail(function() {
showCriticalErrorMessage("bar");
hideLoadingBar();
});
}
/**
* INITIALIZATION STEP 3
* @param {function} callback
* @returns {undefined}
*/
function step3(callback) {
$.ajax({
"url": "justOtherUrl",
"type": "GET",
"dataType": "json",
"contentType": "application/json; charset=utf-8"
}).done(function (data) {
// do stuff with data, with error verification, then...
step4(callback);
}).fail(function() {
showCriticalErrorMessage("baz");
hideLoadingBar();
});
}
/**
* INITIALIZATION STEP 4
* @param {function} callback
* @returns {undefined}
*/
function step4(callback) {
$.ajax({
"url": "anotherUrl",
"type": "GET",
"dataType": "json",
"contentType": "application/json; charset=utf-8"
}).done(function (data) {
callback();
hideLoadingBar();
}).fail(function() {
showCriticalErrorMessage("qux");
hideLoadingBar();
});
}
// then i need to call step1 inside some controller
好的,但如何使其可重复使用troughout许多应用程序? – 2015-02-10 12:24:41
修复了它,现在你可以拥有通用模块,你可以包含在任何angularjs应用程序中 – Marcus 2015-02-10 12:36:18