代码不断返回“Uncaught SyntaxError:Unexpected token”错误

时间:2016-03-10 20:55:05

标签: javascript angularjs

我一直遇到Uncaught SyntaxError: Unexpected token错误,但我看不到我错过的地方/有额外的;)字符。它在代码中给了我一行(见下文),但出现是一个有效的令牌放置。

JSLint给了我一些澄清:Expected ')' to match '(' from line 2 and instead saw ';'.

下面是我正在编写的JavaScript代码(为简洁起见删除了内部部分):

'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

  }
})();

2 个答案:

答案 0 :(得分:2)

括号错了,见图。我有一段时间休息。

enter image description here

答案 1 :(得分:0)

非常感谢@MikeC帮助他解决问题。虽然缩进是问题的关键部分,但它并没有解决潜在的问题:如何定义变量AppCtrl。

要解决这个问题,让我们看一下可以定义AppCtrl的各种方式:

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

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

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

我们目前正在使用第三个定义,现在对我们不利。第一个定义似乎更接近我们想要的。考虑到这一点......

'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
            }
  }]);

......这是正确的配置。

请注意,我们删除了JS文件最顶端的(function() {块,以及最底层的相应关闭元素。我们还将$scope.list和其他$scope函数移到了文件的.controller部分。