我有一个包含对象的数组,每个对象都包含我的菜单信息。
在图像中,您可以看到其中一个对象包含:id, lang, link, modul_id,shrtModul, sort
。
现在我的问题是
当我console.log($scope.usrModules)
并点击第三个对象
我看到键值为1的键排序。
但是当我console.log($scope.usrModules[2]);
有所有信息,但排序键和值以某种方式丢失。
当我console.log($scope.usrModules[2].sort);
日志返回undefined
。
我不确定为什么会这样,但也许有人可以帮助我。
由于我无法在此处发布图片链接myProblem
我的控制器
$scope.usrModules = ParameterService.setMenu(Ls.getObject('mandants'), Ls.getObject('mandantId'));
console.log($scope.usrModules);
console.log($scope.usrModules[2]);
console.log($scope.usrModules[2].sort);
这是我的服务,我在最后建立对象数组我返回arrModules,这是$ scope.usrModules。
.factory('ParameterService', function ($http, Ls, $location, $filter, CommonService) {
var arrModules;
var modulesOrder = [];
var unique = {};
return {
getAllDamage: function (callback) {
$http.get(Ls.get('persist_ipAddress') + "ExternalParameter/GetAllParameters?businessUnitId=" + Ls.getObject("mandantId"))
.success(callback);
},
// does load the specific modules the user is allowed to use
setMenu: function (mandants, mandantId) {
// array of language keys which are used in this view !IMPORTANT -> needs to be in the ascending order as in the language object
var arrLang = ['btnDatenLadenML', 'btnDatenSendenML', 'btnMenuABML', 'btnMenuALML', 'btnMenuBLML', 'btnMenuELML', 'btnMenuGUML', 'btnMenuHMML', 'btnMenuIVML', 'btnMenuKOML', 'btnMenuMAML', 'btnMenuMIML', 'btnMenuSTML', 'btnMenuULML', 'btnMenuVEML', 'btnMenuVPML', 'btnMenuVSML', 'btnMenuWEML', 'btnTourStartML'];
var lang = CommonService.setLang(arrLang);
arrModules = [];
// arr for nested modules 'versand' and ablieferung
arrInnerModules = [];
arrInnerModulesAB = [];
unique = {};
// bool to check if module is already added to the menu
var alreadyAdded = true;
// loop through all mandants, loop through
angular.forEach(mandants, function (value, index) {
if (value.MandantId == mandantId) {
angular.forEach(value.Rechte, function (value, index) {
if (value.option_id === 0) {
var shrt = value.bezeichnung.split("_").pop();
// if the logged in user is an admin (short AM) set a flag
if (shrt == 'AM') {
Ls.set('admin', true);
}
// remove duplicate entries in the modules
if (!unique[value.id]) {
// do not push admin and handheld exit , ablieferung and beladen, verpacken, versandeinheit auflösen modules
if (shrt != 'HE' && shrt != 'AM' && (shrt != 'BL' && shrt != 'VP' && shrt != 'VE') && shrt != 'AB') {
arrModules.push({ id: value.id, modul_id: value.modul_id, shrtModul: shrt, link: '#/' + $filter('lowercase')(shrt) + '10' });
unique[value.id] = value.id;
}
// check for the ABLIEFERUNG module and remove the link - add hardcoded the submenue entries as this are no modules -> ID is just any number
if (shrt == 'AB') {
arrModules.push({ id: value.id, modul_id: value.modul_id, shrtModul: shrt, link: '' });
arrInnerModulesAB.push(
{ id: '991', modul_id: '14', shrtModul: 'TS', link: '#/ab10', lang: lang.btnTourStartML },
{ id: '992', modul_id: '14', shrtModul: 'DL', link: '#/ab60', lang: lang.btnDatenLadenML },
{ id: '993', modul_id: '14', shrtModul: 'DS', link: '#/ab70', lang: lang.btnDatenSendenML });
}
// check if one of the three VERSAND modules are active and create a new menu icon
if ((shrt == 'BL' || shrt == 'VP' || shrt == 'VE')) {
// hardcoded versand module as it is just a overmenu entry for the beladen, verpacken and versandeinheit auflösen
if (alreadyAdded) {
arrModules.push({ id: '99', modul_id: '18', shrtModul: 'VS' });
alreadyAdded = false;
}
// push the nested modules in a separate array (beladen, verpacken, versandeinheit auflösen)
arrInnerModules.push({ id: value.id, modul_id: value.modul_id, shrtModul: shrt, link: '#/' + $filter('lowercase')(shrt) + '10' });
}
// add the inner modules to the first hierachy
for (var i = 0; i < arrModules.length; i++) {
if (arrModules[i].shrtModul == 'AB') {
arrModules[i].modules = arrInnerModulesAB;
}
if (arrModules[i].shrtModul == 'VS') {
arrModules[i].modules = arrInnerModules;
}
}
}
}
})
}
})
$http.get(Ls.get('persist_ipAddress') + "ExternalParameter/GetValues?businessUnitId=" + mandantId + "¶meter=Menu")
.success(function (data) {
angular.forEach(data.Result, function (value, index) {
modulesOrder.push({ 'sort': value.Sort, 'id': value.ID });
});
// bring together the modules and the module Order
angular.forEach(arrModules, function (valueA, indexA) {
angular.forEach(modulesOrder, function (valueMO, indexMO) {
if (valueA.modul_id == valueMO.id.slice(-2)) {
valueA.sort = valueMO.sort;
}
// check if there are inner modules and add the sort to the inner modules
if (valueA.modules){
for (var i = 0; i < valueA.modules.length; i++) {
if (valueA.modules[i].modul_id == valueMO.id.slice(-2)) {
valueA.modules[i].sort = valueMO.sort;
}
}
}
})
})
})
// bring together the module menue and the language files
angular.forEach(lang, function (value, index) {
if (value.indexOf('btn')) {
var tmpM = index.slice(-4);
var shortM = tmpM.slice(0, 2);
}
angular.forEach(arrModules, function (value2, index2) {
if (value2.shrtModul == shortM) {
value2['lang'] = value;
}
// check if there are inner modules and add the language to the inner modules
if (value2.modules) {
for (var i = 0; i < value2.modules.length; i++) {
if (value2.modules[i].shrtModul == shortM) {
value2.modules[i].lang = value;
}
}
}
});
});
return arrModules;
}
};
})
我已经尝试将Controller更改为等待$ http请求而没有运气
$scope.usrModules = function () {
ParameterService.setMenu(Ls.getObject('mandants'), Ls.getObject('mandantId'))
.success(function (arrModules) {
var bla = arrModules;
console.log('yea' + bla);
})
.error(function () {
console.log('NOT');
})
}
或者......
$scope.usrModules = function () {
ParameterService.setMenu(Ls.getObject('mandants'), Ls.getObject('mandantId'))
.then(function (httpData) {
console.log(httpData.data);
}, function (httpData) {
console.log('albums retrieval failed.');
});
}