我刚开始用离子框架。有什么办法来实现离子自动完成的文本框?
我已搜查离子论坛也一派,但找不到任何东西。
在codepen你有一个完整example。
<html ng-app="ionicApp">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Ionic Template</title>
<link href="http://code.ionicframework.com/1.0.0-beta.2/css/ionic.css" rel="stylesheet">
<script src="http://code.ionicframework.com/1.0.0-beta.2/js/ionic.bundle.js">
</script>
</head>
<body ng-controller="MyCtrl">
<ion-header-bar class="bar-positive">
<h1 class="title">{{myTitle}}</h1>
</ion-header-bar>
<ion-header-bar class="bar-subheader item-input-inset">
<label class="item-input-wrapper">
<i class="icon ion-ios7-search placeholder-icon"></i>
<input type="search" placeholder="Search" ng-change="search()" ng-model="data.search">
</label>
<button class="button button-clear">
Cancel
</button>
</ion-header-bar>
<ion-content>
<div class="list">
<li class="item" ng-repeat="airline in data.airlines">{{airline.name}}</li>
</div>
</ion-content>
</body>
</html>
它只需要加入NG-点击选项
<li class="item"
ng-repeat="airline in data.airlines"
**ng-click="doSomethingWithSelectedItem(airline)"**>
{{airline.name}}
</li>
您可以用指令的帮助下,如下AngularJS实现自动完成功能。
首先创建控制器来处理类似下面的文本框中输入动作
app.controller('autoCompleteController', function($scope, $window, autoSearchFactory) {
$scope.inputWidth = '200';
$scope.toggleAutoSearch = 'none';
$scope.searchText = '';
$scope.searchData = null;
$scope.initiateAutoSearch = function() {
$scope.toggleAutoSearch = 'visible';
autoSearchFactory.getCountries($scope.searchText).then(function(data) {
$scope.searchData = data;
});
}
$scope.selectedSearchResult = function(input) {
$scope.searchText = input;
$scope.toggleAutoSearch = 'none';
}
});
然后创建的服务来从Web服务的结果。该服务从API JSON格式使用HTTP GET方法获取结果。
app.service('autoSearchFactory', function($http, $q, $window) {
return {
getCountries : function(countryName) {
return $http.get('https://restcountries.eu/rest/v2/name/' + countryName).then(function(response) {
return response.data;
}, function(errResponse) {
console.error('Error while fetching users');
return $q.reject(errResponse);
});
}
}});
然后,最后创建指令
<div class="input-box" style="width: {{inputWidth}}px">
<input type="text" ng-model="searchText" ng-keyup="initiateAutoSearch();" />
<div class="auto-result" style="width: {{inputWidth}}px; display: {{toggleAutoSearch}}">
<ul ng-repeat="var in searchData">
<li ng-click="selectedSearchResult(var.name)">{{var.name}}</li>
</ul>
</div>
</div>
而已。
您还可以在这里找到完整的例子Auto Complete Text Box using AngularJS