从 AngularJS 1.5 升级到 1.7 抛出 "Can't copy! Making copies of Window or Scope instances is not supported"
Upgrading from AngularJS 1.5 to 1.7 throws "Can't copy! Making copies of Window or Scope instances is not supported"
我想复制我的控制器。
我有以下代码(在 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
这失败了:
Can't copy! Making copies of Window or Scope instances is not supported
我试过了Object.copy
但是我需要控制器的原型函数($setValidity
)
我是这样做的:
scope.ctrlCopy = Object.assign(Object.create(ctrl.__proto__), ctrl);
其中Object.create(ctrl.__proto__)
用于创建原型函数
并 assign 创建深拷贝。
我想复制我的控制器。 我有以下代码(在 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
这失败了:
Can't copy! Making copies of Window or Scope instances is not supported
我试过了Object.copy
但是我需要控制器的原型函数($setValidity
)
我是这样做的:
scope.ctrlCopy = Object.assign(Object.create(ctrl.__proto__), ctrl);
其中Object.create(ctrl.__proto__)
用于创建原型函数
并 assign 创建深拷贝。