Upgrading from AngularJS 1.5 to 1.7 throws "Can't copy! Making copies of Window or Scope instances is not supported"

Viewed 1017

I would like to copy my controller. I had the following code (invalid in angular 1.7):

link: function(scope, elm, attrs, ctrl) {
        if (!ctrl) {
            return;
        }

        // Do a copy of the controller
        scope.ctrlCopy = {};
        angular.copy(ctrl, scope.ctrlCopy); // <- fail here

This fails with:

Can't copy! Making copies of Window or Scope instances is not supported

I tried Object.copy but I need a prototype function of the controller ($setValidity)

1 Answers

I do it like this:

scope.ctrlCopy = Object.assign(Object.create(ctrl.__proto__), ctrl);

Where Object.create(ctrl.__proto__) is used to create prototype functions And assign to create the deep copy.

Related