我正在尝试使用角度 $emit 和命令文件在页面加载期间显示微调器。我的模型是:
Model:
model.load = function(){
model.loading = true;
$rootScope.$emit('loadMyPage', model.loading);
return service.getData().then(storesResult, storesFault);
}
var storesResult = function (value) {
model.categoriesLoading = false;
$rootScope.$emit('loadMyPage', model.loading);
model.stores = value.result;
saveData();
};
var storesFault = function (value) {
var data = value.data;
var status = value.status;
model.categoriesLoading = false;
model.stores = null;
$rootScope.$emit('loadMyPage', model.loading);
$rootScope.$emit('systemAlert', {title: 'Loading Error', message: data, status: status, type: 'danger', timeout: 10000, showAsModal: true});
return value;
};
在 app.run 中执行的我的命令文件
command.execute = function () {
$rootScope.$on('loadMyPage', onLoadingChange);
};
var onLoadingChange = function (event, value) {
if (model.loading === true) {
showModal('sections/modals/loading/loading.tpl.html');
} else {
hideModal();
}
};
};
问题是在页面加载过程中,来自 model.load 的 $emit 没有转到 $on 命令。当 $on 被调用时,它是从 storesResult 块中完成的。结果 model.loading 总是假的。这可能是一个异步问题。
欢迎所有建议。