无法读取未定义的属性长度 - AngularJS

时间:2014-09-09 07:14:46

标签: javascript angularjs angularjs-scope

我的一个变量抛出undefined导致错误:

HTML:

<form ng-submit="search()">
   <div class="input-group col-md-6 typeahead-wrapper">
        <input type="text" ng-model="searchkeyword.keystring" class="typeahead">
        <span class="input-group-btn">
           <button class="btn btn-primary" id="goSearch" type="submit">
              <span class="glyphicon glyphicon-search"></span>
           </button>
        </span>
   </div>
</form>

这是控制器内部的功能:

$scope.search = function() {
        //alert($scope.searchkeyword.keystring);
     $http.get('myurl'+$scope.searchkeyword.keystring).then(sucesscalback,errorcalback);
        function sucesscalback(response)
        {
            //some code
        }
        function errorcalback(failure)
        {
            //some code
        }

我正在尝试实现typeahead,它工作正常,但是当我在控制器中粘贴下面的代码时(在搜索功能之后/之前):

$scope.substringMatcher = function(strs) {
        return function findMatches(q, cb) {
            var matches, substrRegex;
            matches = [];
            substrRegex = new RegExp(q, 'i');
            $.each(strs, function(i, str) {
                if (substrRegex.test(str)) {
                    matches.push({ value: str });
                }
            });
            cb(matches);
        };
    };

           $scope.states = ['Connecticut', 'Delaware', 'Wisconsin', 'Wyoming'];
           var myTypeahead =  $('.typeahead').typeahead({
                hint: true,
                highlight: true,
                minLength: 1,
              },
              {
                name: 'states',
                displayKey: 'value',
                source: $scope.substringMatcher($scope.states)
              });

我的keystring返回undefined:

  

$ scope.searchkeyword.keystring

substringMatcher向我看问题。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

通过将substringMatcher包装在其中来解决问题:

$.noConflict();
    jQuery( document ).ready(function( $ ) {
      // My code for substringMatcher
}

来源here

相关问题