AngularJS ng-options错误:无法复制!不支持制作Window或Scope实例的副本

时间:2017-03-24 03:42:42

标签: javascript angularjs ng-options

我正在使用AngularJS 1.5.11版。

我使用ng-options创建了一个select(multiple)元素:

<select multiple class="form-control" ng-model="vm.selectedBuildings" ng-options="building as building.name for building in vm.buildings track by building.id"></select>

每当我选择其中一个元素,而不是将元素推送到selectedBuildings数组时,我会收到错误:“无法复制!不支持制作Window或Scope实例的副本。”

我花了无数个小时试图解决这个问题。有人请帮忙!

来自控制器的相关代码

var self = this;
self.selectedBuildings = [];

function getBuildings() {
    buildingService.getBuildings(campusId, function (buildings) {
        // retrieve buildings from API
        self.buildings = buildings;

        // etc, etc
    });
}

// I am using a Bootstrap modal to display some information.
// I have multiple structures but only one modal, so when the
// modal is shown, I dynamically populate self.selectedBuildings.
$("#modal-lot").on("shown.bs.modal", function () {
    // etc, etc
    var lot = self.lots[self.structureToUpdate];

    for (var i = 0; i < self.buildings.length; i++) {
        let building = self.buildings[i];
        let index = lot.buildings.indexOf(building.id);
        if (index > -1) {
            self.selectedBuildings.push(building);
        }
    }
    // etc, etc

    $scope.$apply();
});

// When the modal is hidden, clear the array
$("#modal-lot").on("hidden.bs.modal", function () {
    self.selectedBuildings = [];
});

错误讯息

Error: [ng:cpws] Can't copy! Making copies of Window or Scope instances is not supported.
http://errors.angularjs.org/1.5.11/ng/cpws
http://***/static/bower_components/angular/angular.js:68:32
copyElement@http://***/static/bower_components/angular/angular.js:961:21
copyRecurse@http://***/static/bower_components/angular/angular.js:933:41
copyRecurse@http://***/static/bower_components/angular/angular.js:933:41
copyRecurse@http://***/static/bower_components/angular/angular.js:933:41
copyRecurse@http://***/static/bower_components/angular/angular.js:933:41
copyRecurse@http://***/static/bower_components/angular/angular.js:922:37
copyRecurse@http://***/static/bower_components/angular/angular.js:933:41
copyRecurse@http://***/static/bower_components/angular/angular.js:933:41
copyRecurse@http://***/static/bower_components/angular/angular.js:933:41
copyRecurse@http://***/static/bower_components/angular/angular.js:922:37
copyRecurse@http://***/static/bower_components/angular/angular.js:933:41
http://***/static/bower_components/angular/angular.js:29665:91
forEach@http://***/static/bower_components/angular/angular.js:325:24
readNgOptionsMultiple@http://***/static/bower_components/angular/angular.js:29663:18
http://***/static/bower_components/angular/angular.js:31995:57
$apply@http://***/static/bower_components/angular/angular.js:18094:30
http://***/static/bower_components/angular/angular.js:31994:21
dispatch@https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:3:10320

2 个答案:

答案 0 :(得分:0)

尝试将self.selectedBuildings初始化为控制器开头的数组,如下所示

self.selectedBuildings = [];

答案 1 :(得分:0)

您应该在$scope上初始化this,而不是$scope.selectedBuildings = [];

mov eax, 4