Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I keep running into Uncaught SyntaxError: Unexpected token errors, but I can't see where I'm missing/have an extra ; or ) character. It gives me a line in the code (see below), but it appears to be a valid token placement.

JSLint gives me some clarification: Expected ')' to match '(' from line 2 and instead saw ';'.

Below is the JavaScript code I'm writing (inner sections removed for brevity):

'use strict';
(function() {
    AppCtrl = function($scope) {
      //JSON data
    };
    window.AppCtrl = AppCtrl;
  }; //Error shows up on this line
  // Declare app level module which depends on views and components
  angular.module('careApp', [
    //Dependencies
  ])
  .config(['$routeProvider', function($routeProvider) {
    //if given invalid partial, go back to appLaunch.html
    $routeProvider.otherwise({
      redirectTo: '/appLaunch'
    });
  }])
  .controller('AppCtrl', ['$scope', window.AppCtrl]);

  var patientID = ''; $scope.idStore = function() {
    //stuff
  }
  $scope.patientTime = function() {
    //more stuff

  }
})();
share|improve this question
5  
Yeah, that line shouldn't be there. – elclanrs Mar 10 at 20:56
2  
}; is ending your function: so its basically (function(){}; which is invalid syntax because you cannot have multiple statements inside (). You already closed off AppCtrl above the window.AppCtrl = line – Patrick Evans Mar 10 at 20:59
2  
The indentation in the line above that is not right, that's what's confusing you. – elclanrs Mar 10 at 21:02
2  
I've correct the indentation in your code. Now you should be able to see that you have an extra closing brace after window.AppCtrl = AppCtrl. You've already closed off the AppCtrl function and now you're trying to close something else. – Mike C Mar 10 at 21:05
1  
@Thassa You never declared AppCtrl. Put a var in front of AppCtrl. (var AppCtrl = ...) – Mike C Mar 10 at 21:14

Brackets are wrong, see picture. I had some time off.

enter image description here

share|improve this answer
up vote 0 down vote accepted

Many thanks to @MikeC for his help in solving the problem. While the indentation was a critical part of the issue, it didn't solve the underlying problem: how the variable AppCtrl was defined.

To solve it, let's look at the various ways we could define AppCtrl:

app.controller('MyCtrl', ['$scope', function ($scope) {...}])

app.controller('MyCtrl', function ($scope) {...})

var MyCtrl = function ($scope) {...})

We are currently using the third definition, which isn't working in our favor now. The first definition appears to be closer to what we want. With that in mind...

'use strict';

  // Declare app level module which depends on views and components
  angular.module('careApp', [
     //Dependencies
  ])
  .config(['$routeProvider', function($routeProvider) {
      //if given invalid partial, go back to appLaunch.html
    $routeProvider.otherwise({redirectTo: '/appLaunch'});
  }])
  .controller('AppCtrl', ['$scope', function($scope){
            $scope.list = [
               //JSON data
            ];
            var patientID = '';
            $scope.idStore = function() {
             //Stuff
            }
            $scope.patientTime = function(){
              //More stuff
            }
  }]);

...Here's the correct configuration.

Note that we removed the (function() { block up at the very top of the JS file, as well as the appropriate closing elements at the very bottom. We also moved the $scope.list and other $scope functions into the .controller portion of the file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.