为自定义dirctive获取“Uncaught SyntaxError:Unexpected token”

时间:2016-05-22 11:51:46

标签: javascript angularjs

我收到客户指示自动完成的错误Uncaught SyntaxError: Unexpected token )。它在Chrome浏览器的控制台中提供 VM80623:1 Uncaught SyntaxError: Unexpected token )。当我点击VM80623:1时,它会在文件名 VM80623

中提供void();

我从以下链接实现以下指令,有同样的错误:输入任意字符,自动完成搜索框并选择,你会得到同样的错误..

链接:http://www.jitendrazaa.com/blog/salesforce/ajax-based-autocomplete-typeahead-directive-in-angularjs/

(function () {
    'use strict';
    var app = angular.module('app');
    app.directive('Autocomplete', ['Authentication', '$http', function(AuthenticationService, $http){
        return {
            restrict : 'AEC',
            require: 'ngModel',
            scope: {
              modeldisplay:'= modeldisplay'
            },
            templateUrl: 'directives/autocomplete/autocomplete.html',
            link: function(scope, element, attrs, ctrl){
              scope.searchCustomer = function(customerSearch){
                  var params = {
                    'session_key': Authentication.GetSessionKey(),
                    'q': customerSearch
                  };
                  if (!customerSearch){
                    return;
                  }
                  var url = config.url+'/api/search';
                  return $http.post(url, params).then(function(response){
                        var data = response.data;
                        if(data.error == 0) {
                          scope.TypeAheadData = data.result;
                          return data.result;
                        }
                      });
              }
              scope.handleSelection = function(item){
                ctrl.$setViewValue(item);
                scope.modeldisplay = item;
                scope.selected = true;
              };
              scope.isCurrent = function(index) {
                return scope.current == index;
              };
              scope.setCurrent = function(index) {
                scope.current = index;
              };
            }
          };
      }]);
  })();

2 个答案:

答案 0 :(得分:0)

我将您的代码复制并粘贴到IntelliJ中;问题似乎是这一行:

var url = config.url'/api/search';

你需要做一些事情来连接变量和字符串,可能是这样的:

var url = config.url + '/api/search';

还有一些其他小的语法错误;但我没想到会阻止代码运行。

在此片段中:

scope: {
  modeldisplay:'= modeldisplay',
},

不需要逗号:

scope: {
  modeldisplay:'= modeldisplay'
},

在searchCustomer方法的末尾添加分号:

scope.searchCustomer = function(customerSearch){
  // other code here
};

链接功能结束时不需要逗号:

            } // remove comma here,
        };
    }]);
})();

最后,IntelliJ还在$ http.post结果函数中标记了else返回:

                    return $http.post(url, params).then(function(response){
                        var data = response.data;
                        if(data.error == 0) {
                            scope.TypeAheadData = data.result;
                            return data.result;
                        }
/* not needed because we are at end of method 
                       else {
                            return;
                        } */
                    });

以下是完整更新的代码:

(function () {
    'use strict';
    var app = angular.module('app');
    app.directive('Autocomplete', ['Authentication', '$http', function(AuthenticationService, $http){
        return {
            restrict : 'AEC',
            require: 'ngModel',
            scope: {
                modeldisplay:'= modeldisplay'
            },
            templateUrl: 'directives/autocomplete/autocomplete.html',
            link: function(scope, element, attrs, ctrl){
                scope.searchCustomer = function(customerSearch){
                    var params = {
                        'session_key': Authentication.GetSessionKey(),
                        'q': customerSearch
                    };
                    if (!customerSearch){
                        return;
                    }
                    var url = config.url + '/api/search';
                    return $http.post(url, params).then(function(response){
                        var data = response.data;
                        if(data.error == 0) {
                            scope.TypeAheadData = data.result;
                            return data.result;
                        }
/*                        else {
                            return;
                        } */
                    });
                };
                scope.handleSelection = function(item){
                    ctrl.$setViewValue(item);
                    scope.modeldisplay = item;
                    scope.selected = true;
                };
                scope.isCurrent = function(index) {
                    return scope.current == index;
                };
                scope.setCurrent = function(index) {
                    scope.current = index;
                };
            }
        };
    }]);
})();

答案 1 :(得分:0)

问题与以下几行有关:

  1. 范围:{           modeldisplay:' = modeldisplay',//这个额外的,需要删除         }

  2. var url = config.url' / api / search' // config.url& ' / API /搜索'需要连接

  3. 下面附有正确的代码:

     (function () {
        'use strict';
        var app = angular.module('app');
        app.directive('Autocomplete', ['Authentication', '$http', function(AuthenticationService, $http){
            return {
                restrict : 'AEC',
                require: 'ngModel',
                scope: {
                  modeldisplay:'= modeldisplay'
                },
                templateUrl: 'directives/autocomplete/autocomplete.html',
                link: function(scope, element, attrs, ctrl){
                  scope.searchCustomer = function(customerSearch){
                      var params = {
                        'session_key': Authentication.GetSessionKey(),
                        'q': customerSearch
                      };
                      if (!customerSearch){
                        return;
                      }
                      var url = config.url + '/api/search';
                      return $http.post(url, params).then(function(response){
                            var data = response.data;
                            if(data.error == 0) {
                              scope.TypeAheadData = data.result;
                              return data.result;
                            }
                            else {
                              return;
                            }
                          });
                  }
                  scope.handleSelection = function(item){
                    ctrl.$setViewValue(item);
                    scope.modeldisplay = item;
                    scope.selected = true;
                  };
                  scope.isCurrent = function(index) {
                    return scope.current == index;
                  };
                  scope.setCurrent = function(index) {
                    scope.current = index;
                  };
                },
              };
          }]);
      })();