Angular js搜索输入框

时间:2015-05-15 11:34:21

标签: angularjs javascript-events event-handling

我在搜索输入框下方

<div id="query-wrap">
            <input ng-enter="startSearch('{{queryObj.query}}')" id="query" type="text" ng-model="queryObj.query"
                placeholder="Enter Query" autocomplete="off"  />
</div>
<a class="searchButton" ng-click='startSearch(queryObj.query)'>Search</a>

点击搜索我正在调用startsearch函数

$scope.startSearch = function(query) {
            console.log("query is ::" + query);

            //  $location.path("/view");
                $location.search({
                                    q:query
                });

                getData();

}

问题是:点击提交后,正确的查询参数会附加在网址中,但有时在搜索框中输入的查询会逐渐消失,因为我无法看到正确的结果。

以下是我的ng-enter指令。

app.directive('ngEnter', function () {
    return function (scope, element, attrs, dropdownService) {
        element.bind("keydown keypress", function (event) {

            if(event.which === 13) {  
                //If user presses an enter then fire an ESC event to not to show a dropdown
                var e = angular.element.Event("keydown");                    
                e.which = 27;
                element.trigger(e);                  
                scope.$eval(attrs.ngEnter);               
                event.preventDefault();
            }
        });
    };
  });

1 个答案:

答案 0 :(得分:0)

startSearch('{{queryObj.query}}')应为startSearch(queryObj.query)。没有理由把它包装在表达式中。

相关问题