Thassa
Thassa

Reputation: 542

Code keeps returning "Uncaught SyntaxError: Unexpected token" errors

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

  }
})();

Upvotes: 0

Views: 4331

Answers (2)

Thassa
Thassa

Reputation: 542

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.

Upvotes: 0

celerno
celerno

Reputation: 1387

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

enter image description here

Upvotes: 3

Related Questions