0

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

  }
})();
10
  • 5
    Yeah, that line shouldn't be there. Commented Mar 10, 2016 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 Commented Mar 10, 2016 at 20:59
  • 2
    The indentation in the line above that is not right, that's what's confusing you. Commented Mar 10, 2016 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. Commented Mar 10, 2016 at 21:05
  • 1
    @Thassa You never declared AppCtrl. Put a var in front of AppCtrl. (var AppCtrl = ...) Commented Mar 10, 2016 at 21:14

2 Answers 2

3

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.