如何从ng repeat更改文本框值

时间:2016-04-25 05:12:42

标签: javascript angularjs

我在html制作了自己的自动填充功能,我使用ng-repeat来显示建议。这是代码:

<div content-for="title">
    <span>Add Friend</span>
</div>
<div class="scrollable">
  <div class="scrollable-content">

    <div class="list-group">
      <div class="list-group-item">
          <form class="form-inline" role="form">
              <div class="form-group">
                  <label class="sr-only" for="exampleInputEmail2">Email</label>
                  <input type="text" class="form-control" data-ng-model="add.email" id="exampleInputEmail2" placeholder="Email" auto-complete autocomplete="off"/>
                  <div  href="#" ng-repeat="x in cobas | filter:add.email"> 
                    <div class="media-body" ng-if="add.email.length > 0">
                    <h5 class="list-group-item media">{{x.name}}
                    </div></div>
              </div>
              <button type="submit" class="btn btn-default" data-ng-click="addfriends()">Add</button>
          </form>
      </div>
    </div>
  </div>
</div>

我想要的是,当我点击建议结果时,使用textboxng-click的值更改为建议结果,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您必须在控制器中添加功能,如下所示:

$scope.autocomplete = function (completeText) {
   $scope.add.email = completeText;
};

然后在html中调用ng-click上的函数,如下所示,

<div  href="#" ng-repeat="x in cobas | filter:add.email" ng-click="autocomplete(x.name)"> 
     <div class="media-body" ng-if="add.email.length > 0">
           <h5 class="list-group-item media">{{x.name}}</h5
     </div>
</div>