简体   繁体   English

角度ng-options选择未设置为初始值

[英]Angular ng-options select not being set to initial value

I am sending a JSON object in order to initialize some input and select values on my page. 我正在发送JSON对象,以便初始化一些输入并选择页面上的值。 The object holds a ShipTo property. 该对象包含ShipTo属性。 and I want the dropdown to be populated with this initial value. 我希望使用此初始值填充下拉列表。 For some reason it is not populating, even though they are the same object. 由于某些原因,即使它们是同一对象,也不会填充。 I even added in a track by to make sure it is testing equality based on the CompanyName (not exactly sure if thats what track by is meant for) Here is what I have: 我什至添加了一个跟踪器,以确保它正在根据CompanyName测试是否相等(不确定是否是跟踪器的意思)。这就是我所拥有的:

the JSON Object: JSON对象:

{
    "AccountNumber": "12345",
    "Email": null,
    "CompanyName": "HK Electric",
    "Inactive": false,
    "PhoneNumbers": null,
    "ChildOfCustomer": "00000000-0000-0000-0000-000000000000",
    "Id": "a4aab309-321c-4b09-969c-073eb83b90ad",
    "ModifiedBy": null,
    "ModifiedOn": "2014-07-16T18:46:52.065Z",
    "CreatedBy": null,
    "CreatedOn": "2014-07-16T18:46:52.065Z",
    "IsDeleted": false
}

vm.shiptos : vm.shiptos:

[
    {
        "ShipTo": {
            "AccountNumber": "1234",
            "Email": null,
            "CompanyName": "Mk Mechanical",
            "Inactive": false,
            "PhoneNumbers": null,
            "ChildOfCustomer": "00000000-0000-0000-0000-000000000000",
            "Id": "b90f9b8c-3910-43ec-8c14-6294155ce855",
            "ModifiedBy": null,
            "ModifiedOn": "2014-07-15T23:03:58.47Z",
            "CreatedBy": null,
            "CreatedOn": "2014-07-15T23:03:58.47Z",
            "IsDeleted": false
        },
        "Addresses": [],
        "Customer": null,
        "Job": null,
        "Orders": []
    },
    {
        "ShipTo": {
            "AccountNumber": "12345",
            "Email": null,
            "CompanyName": "HK Electric",
            "Inactive": false,
            "PhoneNumbers": null,
            "ChildOfCustomer": "00000000-0000-0000-0000-000000000000",
            "Id": "a4aab309-321c-4b09-969c-073eb83b90ad",
            "ModifiedBy": null,
            "ModifiedOn": "2014-07-16T18:46:52.065Z",
            "CreatedBy": null,
            "CreatedOn": "2014-07-16T18:46:52.065Z",
            "IsDeleted": false
        },
        "Addresses": [],
        "Customer": null,
        "Job": null,
        "Orders": []
    }
]

The ngoptions statement in my HTML: 我的HTML中的ngoptions语句:

<select id="ddlShipto" name="ddlShipto" data-ng-model="vm.Shipto" data-ng-options="shipto as shipto.ShipTo.CompanyName for shipto in vm.shiptos track by shipto.ShipTo.CompanyName" class="form-control FloatLeft" required>
                            <option selected="selected" value="">--Select One--</option>
                        </select>

Not sure track by is not what you're looking for here. 不确定track by不是您在这里寻找的东西。

This is the ng-options syntax you are using: select as label for value in array 这是您使用的ng-options语法: select as label for value in array

The part you have wrong is the select parameter. 您遇到的问题是select参数。 This parameter's expression gets equality checked against the model to determine if it is a match (and will automatically set selected option, etc). 此参数的表达式将与模型进行相等性检查,以确定是否匹配(并将自动设置所选选项,等等)。 So, right now your select parameter is shipto . 因此,现在您的select参数是shipto This means that in order to default to a value, your model needs to be equal to the object in vm.shiptos . 这意味着,为了默认为一个值,您的模型必须等于vm.shiptos的对象。

Here's two options: 这有两个选择:

The first option is to loop through the vm.shiptos and set your model to the one you would like to default. 第一种选择是遍历vm.shiptos并将模型设置为您要默认的模型。 eg: 例如:

angular.forEach($scope.vm.shiptos,function(el,i){
  //check objects against the JSON object and set the model to the object if desired
  if (el.ShipTo.AccountNumber === jsonObject.AccountNumber){
    theModelForNgOptions = el;
  }
};

The second option you have is to change the ng-options select parameter to something else on the object, so that the model doesn't have to be the whole object. 您拥有的第二个选项是将ng-options select参数更改为对象上的其他内容,从而模型不必是整个对象。 If AccountNumber is your unique identifier to those objects, you could change your options to: 如果AccountNumber是这些对象的唯一标识符,则可以将选项更改为:

data-ng-options="shipto.ShipTo.AccountNumber as shipto.ShipTo.CompanyName for shipto in vm.shiptos

Now, the select will default only if the attached model is equal to shipto.AccountNumber . 现在,仅当附加模型等于shipto.AccountNumber ,select才会默认。 Here is an example jsfiddle of this at work: http://jsfiddle.net/ADukg/5392/ 这是工作中的示例jsfiddle: http : //jsfiddle.net/ADukg/5392/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM