在IE上运行时,'Promise'是未定义的错误

时间:2016-06-28 07:25:27

标签: angularjs angular-controller

我有一个在Chrome上运行良好的控制器代码,但在IE 10上运行时,相同的代码返回

ReferenceError: 'Promise' is undefined

返回错误的函数是:

new Promise(function(resolve) {
    MetaModel.load($scope, (regionExist ? reqParmRegion[1] : reqParmRegion), (screenExist ? reqParmScreen[1] : reqParmScreen), resolve);
}).then(function(){

    loadRelationshipByStep($scope.preStep);
     if($rootScope.regionId === 'us') {
        $rootScope.currRel = 'itself';
    } 

    if($rootScope.screenId.indexOf('search') !== -1 ){
       EnumerationService.loadEnumerationByTab();
    }  
    // load data for tab click
    if($rootScope.currRel !== 'undefined' && $rootScope.currRel !== 'itself' && $scope.regionId !== 'us'){
        $scope.loadDataByTab($rootScope.currRel);
    } else if($rootScope.resourceHref !== undefined) {
        var params = {};
         resourceFactory.get($rootScope.resourceHref, params, $rootScope.headers).success(function(responseData){
            var data = responseData.data || responseData;
            if (data) {
                $scope.data=data;
                EnumerationService.executeEnumerationFromBackEnd(data, 'create');
                if($rootScope.regionId === 'us'){
                    EnumerationService.executeEnumerationFromBackEnd(data, 'fetch');    
                }
            }
        });            
    }
});

我是否需要添加任何$ promise变量?

3 个答案:

答案 0 :(得分:3)

IE不支持原生javascript Promise原生。请参阅browser compatibility on MDN

Angular包含提供promise功能的$q服务。您可以使用$q.defer()创建延迟对象,并从该对象返回promise。

我认为等效的代码是:

// create the deferred object
var deferred = $q.defer();

// pass the resolve method as the callback
MetaModel.load($scope, (regionExist ? reqParmRegion[1] : reqParmRegion), 
    (screenExist ? reqParmScreen[1] : reqParmScreen), deferred.resolve);

// chain actions onto the promise.
deferred.promise.then(function(){

    loadRelationshipByStep($scope.preStep);
     if($rootScope.regionId === 'us') {
        $rootScope.currRel = 'itself';
    } 

    if($rootScope.screenId.indexOf('search') !== -1 ){
       EnumerationService.loadEnumerationByTab();
    }  
    // load data for tab click
    if($rootScope.currRel !== 'undefined' && $rootScope.currRel !== 'itself' && $scope.regionId !== 'us'){
        $scope.loadDataByTab($rootScope.currRel);
    } else if($rootScope.resourceHref !== undefined) {
        var params = {};
         resourceFactory.get($rootScope.resourceHref, params, $rootScope.headers).success(function(responseData){
            var data = responseData.data || responseData;
            if (data) {
                $scope.data=data;
                EnumerationService.executeEnumerationFromBackEnd(data, 'create');
                if($rootScope.regionId === 'us'){
                    EnumerationService.executeEnumerationFromBackEnd(data, 'fetch');    
                }
            }
        });            
    }
});

答案 1 :(得分:0)

https://github.com/stefanpenner/es6-promiseCorrect我,如果我错了,但在我看来,您正在使用Promise的原生实现,但IE尚未支持。< / p>

在这种情况下,请尝试使用polyfill

此外,您可以使用Angular's $q service,这是基于kriskowal's Q module的Promisse模式实现。

示例:

$q(function (resolve, reject) {
    MetaModel.load($scope, (regionExist ? reqParmRegion[1] : reqParmRegion), (screenExist ? reqParmScreen[1] : reqParmScreen), resolve);
}).then(function () {

    loadRelationshipByStep($scope.preStep);
     if($rootScope.regionId === 'us') {
        $rootScope.currRel = 'itself';
    } 

    if($rootScope.screenId.indexOf('search') !== -1 ){
       EnumerationService.loadEnumerationByTab();
    }  
    // load data for tab click
    if($rootScope.currRel !== 'undefined' && $rootScope.currRel !== 'itself' && $scope.regionId !== 'us'){
        $scope.loadDataByTab($rootScope.currRel);
    } else if($rootScope.resourceHref !== undefined) {
        var params = {};
         resourceFactory.get($rootScope.resourceHref, params, $rootScope.headers).success(function(responseData){
            var data = responseData.data || responseData;
            if (data) {
                $scope.data=data;
                EnumerationService.executeEnumerationFromBackEnd(data, 'create');
                if($rootScope.regionId === 'us'){
                    EnumerationService.executeEnumerationFromBackEnd(data, 'fetch');    
                }
            }
        });            
    }
});

答案 2 :(得分:0)

原生promise是旧版浏览器不支持的ES6功能。您需要添加polyfill以支持旧浏览器,例如https://github.com/taylorhakes/promise-polyfill

由于您正在使用AngularJS,因此您可以使用$q服务来创建跨浏览器工作的承诺。