Angular JS服务设置变量正确Javascript未定义
Angular JS Service Setting Variables correctly Javascript undefined
所以我创建了一个angular js模块,并尝试链接一个服务,并使用该服务在整个应用程序中共享变量。一切都很好,但当我试图将子菜单[1]设置为对象时,它会说子菜单[1]未定义。没错,它还没有创建。但我只想将子菜单[1]设置为等于一个对象。不明白为什么必须创建它。不确定为什么它说Uncaught TypeError:无法读取未定义的属性子菜单。我console.log我打开的menuArray[key],它返回时未定义。但这也没有道理。
setSubMenu:函数(键,subKey,值({console.log(sharedVariables.menuArray[key](;sharedVariables.menuArray[key].subMenu[subKey]=值;}
这是我所拥有的:
var myModule = angular.module('companiesApp', ['ngResource']).config(function($routeProvider) {
$routeProvider
.when ('/', {controller:ListCtrl, template: $('#companies-list').html()})
.when ('/new', {controller:NewCtrl, template: $('#companies-edit').html()})
.when('/newMenu/:companyID', {controller:EditMenuCtrl, template: $('#menu-edit').html()})
.when ('/edit/:companyID', {controller:EditCtrl, template: $('#companies-edit').html()})
.when ('/editMenu/:companyID', {controller:EditMenuCtrl, template: $('#menu-edit').html()})
.when ('/settings/:companyID', {controller:SettingsCtrl, template: $('#companies-settings').html()})});
myModule.service('sharedVariables', function(){
var sharedVariables = { count : 0, originalCount : 0, menuArray : []};
return{
getOriginalCount : function(){
return sharedVariables.originalCount;
},
setOriginalCount : function(value){
sharedVariables.originalCount = value;
},
getCount : function(){
return sharedVariables.count;
},
setCount : function(value){
sharedVariables.count = value;
},
getMenu : function(){
return sharedVariables.menuArray;
},
setMenu : function(key, value){
sharedVariables.menuArray[key] = value;
},
setMenuName : function(key,value){
sharedVariables.menuArray[key].name = value;
},
setMenuEndPos : function(key, posLeft, posTop){
sharedVariables.menuArray[key].endPosLeft = posLeft;
sharedVariables.menuArray[key].endPosTop = posTop;
},
setSubMenu : function(key, subKey, value){
sharedVariables.menuArray[key].subMenu[subKey] = value;
}
};
});
function EditMenuCtrl($scope,$routeParams, $location, sharedVariables){
var count = 0;
$scope.companyID = $routeParams.companyID;
$scope.menuArray = [];
$.ajax({
url : "companies.php",
type : "post",
data : {
action : "getMenus",
id : $routeParams.companyID
},
success : function(data,status){
var responses = JSON.parse(data);
$('#menu').empty();
for(i in responses){
//if the Root Order LI has not been created, create it
if($('#li-' + responses[i].rootORDER).length == 0){
count++;
var html = '<li id="li-' + responses[i].rootORDER + '" class="dropdown">' + '<a id="link-' + responses[i].rootORDER;
html += '" role="button" class="dropdown-toggle" data-type="text" data-toggle="dropdown">' + responses[i].menuROOT;
html += '</a>' + '<ul id="ul-' + responses[i].rootORDER;
html += '" class="dropdown-menu" role="menu" aria-labelledby="dLabel" ><li role="presentation"><a href="javascript:void(0)" onclick="addLITOUL()" role="menuitem"><i class="icon-plus-sign"></i></a></li></ul></li>';
$('#menu').append(html);
//Record the Start Position of the Element
var Start = $('#li-' + responses[i].rootORDER).position();
sharedVariables.setMenu(responses[i].rootOrder, {name : responses[i].menuROOT, startPosLeft : Start.left, startPosTop : Start.top, subMenu : []});
//Make the Title Editable
$('#link-' + responses[i].rootORDER).editable(function(value,settings){
var tempID = $(this).attr('id');
tempID = tempID.substring(5);
sharedVariables.setMenuName(tempID,value);
return(value);
});
//Make the DropDowns and make them draggable
$('.dropdown-toggle').dropdown();
$('.dropdown').draggable({
containment: "#container",
scroll: false,
snap: true,
stop: function(event, ui) {
var Stoppos = $(this).position();
$("#position").text("STOP: 'nLeft: "+ Stoppos.left + "'nTop: " + Stoppos.top);
var tempID = $(this).attr("id");
tempID = tempID.substring(3); //remove li- to get the index into the array
sharedVariables.setMenuEndPos(tempID, Stoppos.left, Stoppos.top);
}
});
}//end of if
}//end of (for i in responses)
sharedVariables.setOriginalCount(count);
sharedVariables.setCount(count);
//Fill in the inner UL with it's links
for(i in responses){
html = '<li id="' + i + '-' + responses[i].menuTITLE+ '" role="presentation"><a role="menuitem" href="javascript:void(0)">' + responses[i].menuTITLE + '</a>';
html += '</li>';
$('#ul-' + responses[i].rootORDER).append(html);
sharedVariables.setSubMenu(responses[i].rootORDER, responses[i].linkORDER, {name: responses[i].menuTITLE});
}
}
});
console.log(sharedVariables.getOriginalCount());
}
我敢说,您的问题与您使用键responses[i].rootOrder
调用setMenu
和使用不同键responses[i].rootORDER
调用setSubMenu
有关。从代码的其余部分来看,responses[i].rootOrder
可能是未定义的,这应该会在其他地方产生奇怪的结果。。。
相关文章:
- JavaScript未定义的函数外部JavaScript文件
- 函数中的Javascript未定义数组
- Javascript-未定义ReferenceError x(函数)
- JSON JavaScript 未定义的变量问题
- Javascript 未定义错误:引用错误:未定义 ALT
- Javascript 未定义函数错误
- 通过使用JavaScript“未定义”(变量),我们可以释放系统内存
- javascript:未定义获取对象
- JavaScript 未定义的变量从页面类中获取值
- Javascript:未定义不是函数错误
- Javascript 未定义的函数错误
- JavaScript“未定义提示符”引用错误
- Javascript未定义错误:“this”为null
- VS中出现JavaScript未定义错误
- Javascript未定义的变量为空
- 在regex拆分过程中会出现javascript未定义元素
- Javascript未定义、空字符串和if语句
- 使用prototype[javascript]未定义的结果
- 匿名函数中的JavaScript未定义变量
- JavaScript:未定义的值赋值