首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“`ng blur`”

“`ng blur`”
EN

Stack Overflow用户
提问于 2016-08-25 05:15:16
回答 2查看 1.5K关注 0票数 2

在角应用程序中,对于搜索字段,我使用来自控制器的过滤器函数。它工作得很好,但是我无法用当前的模型实现ng-blur

我不知道为什么这不管用,有人帮我吗?

同时,我在两个单独的行中显示数据(根据客户请求),请评估我的显示方法,如果有人知道更好的方法,请告诉我。

使用“模糊”时,单击“列表无所选”:

代码语言:javascript
复制
 <div class="child">
      <input type="text" ng-model="search" 
      ng-focus="isDropDown = true"
      ng-blur="isDropDown = false" //nothing selected if blur exist
      ng-keyup="village='';isDropDown = true"
      >

这是我的代码:

html:

代码语言:javascript
复制
<div  class="parent" >
    <div class="child">
      <input type="text" ng-model="search" 
      ng-focus="isDropDown = true"
      ng-keyup="village='';isDropDown = true"
      >
      <span>{{village | uppercase }}</span>
    </div>
    <table ng-show="isDropDown" ng-mouseleave="isDropDown = false">
      <tbody>
        <tr ng-repeat="item in items" ng-click="select(item);">
          <td>{{item.id}}</td>
          <td>{{item.name}}</td>
        </tr>
      </tbody>
    </table>
  </div>

Js部分:

代码语言:javascript
复制
$scope.items2 = $scope.items;

  $scope.isDropDown = false;

  $scope.$watch('search', function(val) {
    $scope.items = $filter('filter')($scope.items2, val);
  });

  $scope.select = function(item) {

    $scope.isDropDown=false; // how to put this in inline? ( html )
    $scope.search = item.name;
    $scope.village = item.village;

  }

现场演示

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-25 06:08:12

移除手表,您可以使用filterblur来防止单击事件,因此在单击时禁用。

溶液

代码语言:javascript
复制
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope, $filter) {
  $scope.items = [{
      id: 1,
      name: 'John',
      village: 'kongambattu'
    }, {
      id: 2,
      name: 'Steve',
      village: 'Serivanthadu'
    }, {
      id: 3,
      name: 'Joey',
      village: 'moolapakkam'
    }, {
      id: 4,
      name: 'Mary',
      village: 'Sooramangalam'
    }, {
      id: 5,
      name: 'Marylin',
      village: 'Kariyamanikkam'
    }, {
      id: 6,
      name: 'Arif',
      village: 'madukarai'
    }

  ];

  $scope.items2 = $scope.items;

  $scope.isDropDown = false;


  $scope.select = function(item) {
    $scope.isDropDown=false; // how to put this in inline? ( html )
    $scope.search = item.name;
    $scope.village = item.village;

  };

});
代码语言:javascript
复制
.parent{
  width:15em;
  border:1px solid gray;
  position:relative;
}

.parent span{
  display: block;
  font-size: 0.75em;
  padding:0.5em;
}

.parent input{
  width:100%;
  padding:0.5em;
  margin:0;
  box-sizing:border-box;
  border:0;
  outline: none;
}

table{
  border:1px solid red;
  width:100%;
  position: absolute;
  top:3.1em;
  border-collapse:collapse;
}

table tr td{
  padding:0.5em;
}

table tr:nth-child(odd){
  background:#ccc;
}
代码语言:javascript
复制
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">

  <div  class="parent" >
    <div class="child">
      <input type="text" ng-model="search" 
      ng-focus="isDropDown = true" ng-blur='isDropDown = false' />
      <span>{{village | uppercase }}</span>
    </div>
    <table ng-show="isDropDown">
      <tbody>
        <tr ng-repeat="item in items|filter: search" ng-mousedown="isDropDown = false;select(item)">
          <td>{{item.id}}</td>
          <td>{{item.name}}</td>
        </tr>
      </tbody>
    </table>
  </div>

</body>

</html>

票数 1
EN

Stack Overflow用户

发布于 2016-08-25 06:16:14

你需要改变你的状态ng-mouseleave="isDropDown = true"这里是柱塞https://plnkr.co/edit/tpl:8rFfZljYNl3z1A4LKSL2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39137168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档