意外的标记 (

时间:2015-12-15 19:08:54

标签: angularjs promise ecmascript-6 angular-promise

我遇到以下错误

Uncaught SyntaxError: Unexpected token (
angular.js:4109Uncaught Error: [$injector:modulerr]      
http://errors.angularjs.org/1.3.15/$injector/modulerr?p0=SmashBoard&p1=Erro…    
gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.15%2Fangular.min.js%3A17%3A381)

在我的Angular工厂

angular.module('SmashBoard', [])
.controller('LocationController', function($scope, LocationService){
    LocationService.getGeoLocation()
        .then(function(geoposition){
            $scope.coordinates = geoposition.coords;
            $scope.$digest();
            })
        .catch(function(){
            $scope.coordinates = {latitude:'N/A', longitude:'N/A'};
            $scope.$digest();
        });
})
.factory('LocationService', function($q){

    return 
    {
        getGeoLocation: function() {
            return new Promise(function(resolve, reject){
                window.navigator.geolocation.getCurrentPosition(function(geo){
                    resolve(geo);   
                }, function(positionError){
                    console.debug(arguments);
                    reject();
                });
            })
        }
    };
});

如果我用下面的地方更改工厂并删除`$ scope。$ digest()

.factory('LocationService', function($q) {
    return {
        getGeolocation: function() {    
            var q = $q.defer();
            window.navigator.geolocation.getCurrentPosition(function(geo) {
                    q.resolve(geo);
                }, function(positionError){
                    console.debug(arguments);
                    q.reject();
                });
            return q.promise;
        }
    };
});

它解决了错误。但我不明白我的工厂定义有什么问题。

0 个答案:

没有答案