Angularjs Select 值在以编程方式添加后从 ngModel 中删除

Angularjs Select values are getting removed from ngModel after being added programmicatically

我正在使用 Angularjs 和 select2。我有一个多选框,它根据在另一个下拉列表中选择的值进行填充。我在按钮上使用单击事件来触发一种方法,该方法将 id 列表添加到分配给多选框的 ngModel 对象。在该表单元素上触发更改事件后,我可以看到值已正确添加,因为即使在阅读文章并将 ngModel 值更改为对象而不是数组之后,当我以编程方式添加它们时,这些值仍然没有绑定。在我触发表单元素上的更改事件后,我能够看到一些值,但不是全部。一些值已被删除。下面是我的代码。

addRecord.vm

<div class="col-sm-4">
    <select ui-select2="{ allowClear: false, minimumResultsForSearch: 10, theme: bootstrap }"
        name="recordTemplate" ng-model="addRecordCtrl.selectedTemplate">
        <option class="selectPlaceholder" value="" disabled selected hidden>- Chose Record Template -</option>
        <option ng-repeat="template in addRecordCtrl.recordTemplates" value="{{record.name}}">{{record.name}}</option>
    </select>
</div>

    <div class="col-sm-9">
      <button id="addButton" class="btn btn-primary btn-lg" ng-click="addRecordCtrl.addRecords()" ng-disabled="addRecordCtrl.isLoading 
|| addRecordCtrl.isEdit">Add</button>
 </div>

<div class="col-lg-9 col-xs-9" id="recordContainer" name="recordContainer">
     <select ui-select2="{ allowClear: false, minimumResultsForSearch: Infinity}"
         name="records" id="records" class="medium"
         multiple ng-model="addRecordCtrl.records.id"
         ng-required="true" ng-cloak>
             <option ng-repeat="record in addRecordCtrl.availableRecords | notInArray:addRecordCtrl.selectedRecordIds.ids:'recordId'"
                value="{{record.recordId}}">{{record.name}}</option>
    </select>
    <input type="hidden" ng-model="addRecordCtrl.selectedRecordName" value="">
</div>

app.js

recordApp.controller('RecordController', ['$http', '$window', '$modal', '$log','$filter', '$timeout', function ($http, $window, $modal, $log, $filter, $timeout) {

    var self = this;
    self.availableRecords = [{
        recordId: "1",
        name: "Love and Happiness"
    },{
        recordId: "2",
        name: "Feel the Burn"
    },{
        recordId: "3",
        name: "Juicy Baby"
    }];
    self.recordTemplates = null;
    self.selectedRecordIds = {};

    self.addRecords = function () {

        //add record ids from the selected records.
        self.recordTemplates.recordId.forEach(function (id) {
            self.selectedRecordIds.ids.push(id.recordId);
        });

        self.recordAdded = true;
    };

}]);
//filter to filter out ids that have already been selected.
recordsApp.filter('notInArray', ['$filter', function($filter){
    return function(list, arrayFilter, element){
        if(arrayFilter){
            return $filter("filter")(list, function(listItem){
                for (var i = 0; i < arrayFilter.length; i++) {
                    if (arrayFilter[i][element] == listItem[element])
                        return false;
                }
                return true;
            });
        }
    };
}]);

我的某些值在以编程方式添加后从 selectedRecordIds 数组中删除的原因是因为它们不在 availableRecords 数组中。一旦我确定 availableRecords 数组中的那些值都很好。