我想复制我的控制器。 我有以下代码(在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
此操作失败,并显示以下信息:
无法复制!不支持制作Window或Scope实例的副本
我尝试了Object.copy
,但是我需要控制器($setValidity
)的原型功能
答案 0 :(得分:1)
我这样做是这样的:
scope.ctrlCopy = Object.assign(Object.create(ctrl.__proto__), ctrl);
使用Object.create(ctrl.__proto__)
创建原型函数的位置
然后assign创建深层副本。