简体   繁体   English

传递给函数的对象中的Javascript数组为空

[英]Javascript array in object empty when passed to function

I have a web page that opens an modal page when clicked on "edit". 我有一个网页,单击“编辑”时会打开一个模式页面。 Situation: With an Angular JS ng-repeat I show a list of objects named customer; 情况:使用Angular JS ng-repeat,我显示了一个名为customer的对象的列表;

<div class="col-sm-6" ng-repeat="Cust in Customers | filter: filterCustomers">
//code omitted for clarity
<buton class="btn btn-default btn-xs" ng-click="openEditDialog(Cust);">Edit<i class="fa fa-pencil pull-right"></i></buton>

The object customer is setup this way; 对象客户是通过这种方式设置的;

{
"ID" : 1,
"Name" : "Test",
"City" : "Test",
"CountryCode" : "NLD",
"CurrencyCode" : "EUR",
"empAgencies" : [{
        "ID" : 1,
        "Name" : "Test",
        "NumberOfEmployees" : 0,
        "customers" : []
    }, {
        "ID" : 2,
        "Name" : "Test1",
        "NumberOfEmployees" : 0,
        "customers" : [{
                "ID" : 4,
                "Name" : "TestC",
                "City" : "Test",
                "CountryCode" : "NLD",
                "CurrencyCode" : "EUR",
                "empAgencies" : []
            }
        ]
    }
]
}

Please note that Customer<->EmpAgency is a many to many relationship in my MVC, I don't know if this has anything to do with the problem. 请注意,Customer <-> EmpAgency在我的MVC中是多对多关系,我不知道这是否与问题有关。 The problem is in the openEditDialog(Cust); 问题出在openEditDialog(Cust);中。 function 功能

 function openEditDialog(Cust) {
         $scope.editedCustomer = Cust;
         console.log($scope.editedCustomer);
         $uibModal.open({
             templateUrl: 'scripts/spa/customer/editCustomerModal.html',
             controller: 'editCustomerCtrl',
             scope: $scope
         }).result.then(function ($scope) {
             search($scope.page);
         }, function () {
             search($scope.page);
         });
     }

The following object is passed to this function: 以下对象传递给此函数:

Object {ID: 1, Name: "Test", City: "Test", CountryCode: "NLD", CurrencyCode: "EUR"…}
//omitted
empAgencies:Array[0]

As you can see the Array containing empAgencies is emptied for some reason. 如您所见,包含empAgencies的数组由于某种原因而被清空。 Because of this my dropdownbox selected property is not populated properly.(contains the complete list of empAgencies) 因此,我的dropdownbox selected属性未正确填充。(包含empAgencies的完整列表)

just remove $scope from the callback: 只需从回调中删除$ scope即可:

         }).result.then(function () {
             search($scope.page);
         }, function () {
             search($scope.page);
         });
     }

I've found the solution; 我找到了解决方案; My dropdownbox on the modal page has a new list of empAgencies. 模态页面上的我的下拉框具有新的临时代理列表。 My ng-model is editedCustomer.empAgencies. 我的ng模型是editedCustomer.empAgencies。 Because there is nothing selected in my dropdownbox the customer object automatically loses its value. 因为在我的下拉框中没有选择任何内容,所以客户对象会自动失去其价值。

I still have to see how I will solve this to get the selected value in my select right. 我仍然必须查看如何解决此问题,以在我的选择权中获得所选值。

(edit) I have solved this in the following way; (编辑)我已经通过以下方式解决了这个问题; Because it was a many to many relationship I had to make the arrays the same: 因为是多对多的关系,所以我必须使数组相同:

   var array = new Array();
    for (var i = 0; i < $scope.AgencyDropDown.length; ++i) {
        array.push({
            ID : $scope.AgencyDropDown[i].ID,
            Name : $scope.AgencyDropDown[i].Name
        });
    }
    $scope.AgencyDropDownSubSet = array;
    var array1 = new Array();
    for (var i = 0; i < $scope.editedCustomer.empAgencies.length; ++i) {
        array1.push({
            ID : $scope.editedCustomer.empAgencies[i].ID,
            Name : $scope.editedCustomer.empAgencies[i].Name
        });
    }
    $scope.editedCustomer.empAgencies = array1;

Now the objects are the same the selected item will be filled correctly. 现在,对象相同,所选项目将被正确填充。 Even when I add new variables to the empAgency this select box will keep working. 即使当我向empAgency添加新变量时,此选择框也将继续工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript,将数组传递给函数时不再是数组 - Javascript, Array no longer array when passed into function jQuery Object 传递给 Javascript ZC1C425268E68385D1AB5074C17A94F 时未定义 - jQuery Object undefined when passed to Javascript function 传递给函数时,Javascript对象未定义 - Javascript object undefined when passed to function 我需要一个javascript递归函数,当key和json对象传递给这个函数时返回一个值数组 - I need a javascript recursive function that return an array of values when key and json object is passed to this function 为什么当我尝试访问它的数组时,传递给第二个 function 的 object(包含一个数组)会给出一个空数组? - Why does the object (containing an array) passed to second function, give an empty array when I try to access its array? 传递时对象中的数组为空或 JSON.stringified - Array in object empty when passed or JSON.stringified 通过onClick传递给JavaScript函数的数组正在创建[object,object] - Array Passed to JavaScript Function through onClick is creating [object, object] 传递给函数的对象数组为空 - Array of objects passed to function is empty 传递空字符串时返回空数组 - Return empty array when empty string is passed Javascript object 传入 function 但显然是按值传递 - Javascript object passed in a function but apparently passed by value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM