【问题标题】:how to hide input field whenever clicked anywhere else in angular js每当单击角度js中的其他任何位置时如何隐藏输入字段
【发布时间】:2016-07-09 08:38:20
【问题描述】:

我是一个试图学习一些东西的菜鸟.....我已经编写了一个类似于实时搜索的代码......现在我想在我点击页面上的其他任何地方时隐藏它......代码是使用 Angularjs。完整的代码有点复杂。实时搜索只是其中的一部分。所以它在这里不起作用……谁能给我一个问题的答案?

var app = angular.module('instantsearch', []);

// Defines the search controller by bringing the data into the scope.
//
app.controller("instantSearchCtrl", function($scope) {
  $scope.data = data;

  $scope.setQuery = function(query) {
    $scope.query = query;
    $scope.focus = false;
  };
});

// Returns the search function that will perform the filter on the data.
//
app.filter('search', function() {
  return search;
});

// Returns an array of items where the item text matches the search query. In
// this example, both the query and item are converted to lower case for easier
// matching.
//
function search(arr, query) {
  if (!query) {
    return arr;
  }

  var results = [];
  query = query.toLowerCase();

  angular.forEach(arr, function(item) {
    if (item.toLowerCase()
      .indexOf(query) == 0) {
      results.push(item);
    }
  });

  return results;
};
 

ul.data-ctrl 
{
    
    list-style: outside none none;
    margin: 0 auto;
    max-width: 500px;
    padding-left: 0;
    text-align: center;
    text-decoration: none;
}
input.search {
    border: 2px solid #1fa67a;
    font-size: 16px;
    height: 35px;
    text-align: center;
    width: 280px;
}
.data-ctrl li {
    background-color: #000;
    border-radius: 3px;
    color: #ffffff;
    font-size: 17px;
    height: 25px;
    padding: 0;
}
<tr ng-app="instantsearch">
    <td colspan="3"  ng-controller="instantSearchCtrl">
        <input type="text" class="search" name="keyword" ng-model="query" id="keywords" placeholder="Keywords:" ng-focus="focus=true">
        <ul class="data-ctrl" ng-show="focus">
            <li ng-repeat="item in data | search:query" ng-bind="item" ng-click="setQuery(item)"></li>
        </ul>
    </td>
</tr>

【问题讨论】:

标签: angularjs


【解决方案1】:

您可以尝试将ng-hideng-blur 一起使用,例如:

<tr ng-app="instantsearch">
<td colspan="3"  ng-controller="instantSearchCtrl">
    <input type="text" class="search" name="keyword" ng-model="query" id="keywords" placeholder="Keywords:" ng-focus="focus=true" ng-hide="hideInput" ng-blur="hideInput = true;">
    <ul class="data-ctrl" ng-show="focus">
        <li ng-repeat="item in data | search:query" ng-bind="item" ng-click="setQuery(item)"></li>
    </ul>
  </td>
</tr>

【讨论】:

  • 如果我点击一个项目会发生什么。在点击 li 之前会触发输入模糊。所以我们可能无法在“setQuery”函数中得到“item”。
猜你喜欢
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 2012-09-25
  • 2019-04-05
  • 2021-09-21
相关资源
最近更新 更多