0

I am having following errors

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)

at my Angular Factory

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();
                });
            })
        }
    };
});

If I change the factory with below and remove `$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;
        }
    };
});

It resolves the error. But I am not understanding what's wrong with my factory definition.

njzk2
  • 38,969
  • 7
  • 69
  • 107
Mohit
  • 2,189
  • 4
  • 22
  • 40
  • I will ask a stupid question, did you put the angular.module('name') before the .controller? – Gianmarco Dec 15 '15 at 19:10
  • I think you haven't declare you `SmashBoard` app, it should be `angular.module('SmashBoard',[ ])` – Pankaj Parkar Dec 15 '15 at 19:13
  • for me your code it's working – Gianmarco Dec 15 '15 at 19:14
  • I've reindented your code. Check to make sure it's accurate, but I find this a lot more readable – ryanyuyu Dec 15 '15 at 19:19
  • I had added the module but due to very long code i just pasted short part of it. Let me edit – Mohit Dec 15 '15 at 19:21
  • 3
    I notice you have a `{` under a return statement. Don't do that, put it on the same line. It could end up returning nothing. http://stackoverflow.com/questions/3641519/why-does-a-results-vary-based-on-curly-brace-placement – Seer Dec 15 '15 at 19:26
  • Yes @Seer Thank you it was the case. why the code wasn't working. – Mohit Dec 15 '15 at 19:31

0 Answers0