预先选择后如何清除字段?

时间:2014-09-22 16:48:31

标签: angularjs angular-ui-bootstrap

选择typeahead提供的选项之一后使用ui.bootstrap如何清除输入值?

http://plnkr.co/edit/yz4EtEJSTVekSemzxagC?p=preview

var app = angular.module("app", ['ui.bootstrap']);


app.controller('createBundle', ['$scope', '$http', '$location',
    function($scope, $http, $location){
  $scope.products = [{name: 'One'}, {name: 'Two'}];

  $scope.bundle = {
    name: '',
    products: [],
    technologies: [],
    deliveryMethods: [],
  };

  $scope.addProduct = function(item, model, label){
    $scope.bundle.products.push(item);
  }

  $scope.resetProducts = function(){
    $scope.bundle.products = [];
  }

}]);

HTML

    <input type="text" class="form-control" id="products" 
ng-model="tempProduct" 
typeahead="product.name for product in products | filter:$viewValue | limitTo:8" 
placeholder="Products" typeahead-on-select="addProduct($item, $model, $label)">

解决方案

typeahead-on-select="addProduct($item, $model, $label);tempProduct=null"

2 个答案:

答案 0 :(得分:2)

addProduct功能中,您需要清除输入的模型

$scope.addProduct = function(item, model, label){
  $scope.bundle.products.push(item);
  $scope.tempProduct = null; //-- clear it
}

答案 1 :(得分:1)

我分叉了你的源代码,为你提供了如何做到这一点的建议。

我做了一些改变,这就是结果。 你可以在这个plunker链接中看到它。

$scope.addProduct = function(item){ $scope.bundle.products.push(item); $scope.tempProduct = ""; }

http://plnkr.co/edit/9f7FsNEILQKdDN1GyUp6?p=info