angularJS中搜索框的用法

  angularJS的强大数据绑定功能让一键搜索成为可能,摒弃繁琐的button,让我们一起看一下让喝让搜索逆袭!

  首先,HTML页面引入input,

  

<div class="form-horizontal" ng-controller="ServiceController">
    <form>
<label for="kw" class="sr-only">请输入您要搜索的关键字</label>

<div class="col-xs-4">
<input class="form-control" id="kw" placeholder="search..." ng-model="kw">
</div>
</form>
<button type="text" class="btn btn-warning pull-right header-adjust">导出Excel</button>
</div>

 

 

  当然,借助H5,你的type="search"会更完美一点.

  接下来,你要监听input搜索框的信息  

 

    app.controller('ServiceController', function($scope,) { 
        $scope.$watch('username', function(newUserName) {...}

       }

  这样,你就很好的监听了你的数据变化。但这同时会产生一个问题,高频率的监听会耗损你的效率,下面,我们做最后的完善,让数据"智能"监听。

    

      app.controller('ServiceController', function($scope, $timeout, githubService) { // 添加了$timeout服务
      var timeout;
      $scope.$watch('username', function(newUserName) {

      if (newUserName) { 11 // 如果在进度中有一个超时(timeout)
      if (timeout) $timeout.cancel(timeout);
      timeout = $timeout(function() {

        githubService.events(newUserName)

          .success(function(data,status){

      $scope.events=data.data;

      })

      }, 350); }

      }); }); 

      

  ok,现在,如果用户两次输入之间有350 ms的间 隔,就推断用户已经完成了输入,然后开始向GitHub发送请求 。

  

 

转载于:https://www.cnblogs.com/qinglingyue/p/5163436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值