2014-02-28 144 views
0

相关问题:AngularJS Decorator without object.definePropertyAngularJS装饰IE7

它似乎适用装饰的标准方法是用object.defineProperty但毕竟是not supported in IE7

有一些填充工具选项object.defineProperty

我也做在plunkr了一些有趣的结果一些尝试。

<div ng-controller="ParentCtrl"> 
    <div ng-controller="ChildCtrl"></div> 
</div> 
<div ng-controller="SiblingCtrl"></div> 

var app = angular.module('plunker', []); 

app.config(['$provide', function($provide){ 
    $provide.decorator('$rootScope', ['$delegate', function($delegate){ 
    $delegate.a = 1; 
    $delegate.constructor.prototype.b = 2; 
    Object.defineProperty($delegate.constructor.prototype, 'c', { 
     value: 3 
    }); 
    return $delegate; 
    }]); 
}]); 

app.controller('ParentCtrl', function($rootScope, $scope) { 
    console.info('ParentCtrl', $rootScope.a); // 1 
    console.info('ParentCtrl', $rootScope.b); // 2 
    console.info('ParentCtrl', $rootScope.c); // 3 
    console.info('ParentCtrl', $rootScope.constructor.prototype.a); // undefined 
    console.info('ParentCtrl', $rootScope.constructor.prototype.b); // 2 
    console.info('ParentCtrl', $rootScope.constructor.prototype.c); // 3 
    $rootScope.a = 'a'; 
    $rootScope.b = 'b'; 
    $rootScope.c = 'c'; 
}); 

app.controller('ChildCtrl', function($rootScope, $scope) { 
    console.info('ChildCtrl', $rootScope.a); // 1 
    console.info('ChildCtrl', $rootScope.b); // b 
    console.info('ChildCtrl', $rootScope.c); // 3 
    console.info('ChildCtrl', $rootScope.constructor.prototype.a); // undefined 
    console.info('ChildCtrl', $rootScope.constructor.prototype.b); // 2 
    console.info('ChildCtrl', $rootScope.constructor.prototype.c); // 3 
}); 

app.controller('SiblingCtrl', function($rootScope, $scope) { 
    console.info('SiblingCtrl', $rootScope.a); // a 
    console.info('SiblingCtrl', $rootScope.b); // b 
    console.info('SiblingCtrl', $rootScope.c); // 3 
    console.info('SiblingCtrl', $rootScope.constructor.prototype.a); // undefined 
    console.info('SiblingCtrl', $rootScope.constructor.prototype.b); // 2 
    console.info('SiblingCtrl', $rootScope.constructor.prototype.c); // 3 
}); 

我的问题是:它主要是提供给rootScope的方法类似this answer显示了正确的方式。

回答

0

我这个

app.config(['$provide', function($provide){ 
    $provide.decorator('$rootScope', ['$delegate', function($delegate){ 
    $delegate.func = function() { 
     ... 
    }; 
    return $delegate; 
    }]); 
}]); 

走到这是最简单的解决方案,它在IE7中工作。打电话给constructorprototype__proto__没有或没有polyfills只是导致无尽的麻烦。