Angularjs material 實(shí)現(xiàn)搜索框功能
angular-material 是 AngularJS 的一個(gè)子項(xiàng)目,用來提供實(shí)現(xiàn)了 Material Design 風(fēng)格的組件。
Material 提供了大量的android 風(fēng)格的UI組件,使用 angularjs + Material 可以很容易開發(fā)出風(fēng)格接近原生 Android 5.x 的web界面。但在實(shí)際使用的過程中并不總是能滿足我們的需求。開發(fā)一個(gè)組件就成了我們必須學(xué)習(xí)的內(nèi)容。
下面是一個(gè)組件的實(shí)現(xiàn):
//前面省略若干代碼 directive('mdSearchInput',[function(){ return{ restrict:'E', controller:['$scope','$timeout',function($scope,$timeout){ var tsk=null; $scope.delay=1000; $scope.on_changed=function(){ if(null !== tsk) {$timeout.cancel(tsk);} //去掉前一個(gè)任務(wù) tsk = $timeout(function(){ $timeout.cancel(tsk);tsk=null; $scope.onSearch()($scope.searchText); },$scope.delay); };$scope.on_clear=function(){ var tsk=null;$scope.searchText=''; tsk=$timeout(function(){ $timeout.cancel(tsk);tsk=null; $scope.onSearch()($scope.searchText); },100); } }], scope:{ inputName: '@mdInputName', searchText: '=?mdSearchText', onSearch: '&?mdSearch', placeholder: '@placeholder', delay: '@delay' }, template:'<div class="md-search-input" layout="row">\ <input type="text" flex autocomplete="off" ng-model="searchText" name="{{inputName}}" placeholder="{{placeholder}}" ng-change="on_changed()" />\ <md-button class="md-fab" ng-click="on_clear()" ng-show="searchText!==\'\'"><md-icon md-svg-icon="md-close" style="color:rgba(0,0,0,0.5);"></md-icon></md-button>\ </div>', link:function($scope, $element){ } }; }]);
CSS 樣式:
.md-search-input{ box-sizing: border-box;border: none;box-shadow: none;background: 0 0; border-radius:5px;background: #FFF;margin:0px;position: relative; input{outline: 0;font-size: 14px; width: 100%; padding: 0 15px; line-height: 40px;height: 40px;border: none;background:transparent;} button,.md-fab,.md-button,button:hover,.md-fab:hover { background:transparent !important; line-height:40px;height:40px;width:40px;font-size:14px;box-shadow:none !important;margin:0px;padding:0px; color:rgba(0,0,0,0.5) !important; } }
配合 ng-route 可以很容易實(shí)現(xiàn)無刷新的APP 讓您的web頁面更加接近app體驗(yàn),
在 maincontroll中,通過監(jiān)聽 ng-route 的頁面即將跳轉(zhuǎn)事件 來重置消息框,
//在頁面改變之前,重置搜索框. $scope.$on('SearchText.Reset',function(){ $scope.searchConfig={show:false, key:'',delay:1200};}); $rootScope.$on('$routeChangeStart', function (event, next) { $rootScope.$broadcast('SearchText.Reset'); });
而在需要用到搜索功能的地方,則只需要在控制器里通過如下代碼實(shí)現(xiàn):
//陪值搜索框?yàn)榧河? $scope.$emit('Search.Config',{ show:true, key:'',delay:800, emptyText:"請輸入:商家名稱,賬號,電話 等內(nèi)容以進(jìn)行搜索.", onSearch: function(){ return function(v){ $scope.merData.query(v); //調(diào)用本控制器的數(shù)據(jù)查詢接口. } } });
以上所述是小編給大家介紹的Angularjs material 實(shí)現(xiàn)搜索框功能,希望對大家有所幫助!
- Angular實(shí)現(xiàn)跨域(搜索框的下拉列表)
- Angular實(shí)現(xiàn)搜索框及價(jià)格上下限功能
- Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示效果
- 在 Angular 中實(shí)現(xiàn)搜索關(guān)鍵字高亮示例
- angularjs實(shí)現(xiàn)搜索的關(guān)鍵字在正文中高亮出來
- angularjs實(shí)現(xiàn)分頁和搜索功能
- Angularjs過濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能示例
- angularJs使用$watch和$filter過濾器制作搜索篩選實(shí)例
- Angular封裝搜索框組件操作示例
相關(guān)文章
AngularJS實(shí)現(xiàn)表單驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)表單驗(yàn)證功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Angular使用 ng-img-max 調(diào)整瀏覽器中的圖片的示例代碼
本篇文章主要介紹了Angular使用 ng-img-max 調(diào)整瀏覽器中的圖片的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Angular使用$http.jsonp發(fā)送跨站請求的方法
這篇文章主要介紹了Angular使用$http.jsonp發(fā)送跨站請求的方法,結(jié)合實(shí)例形式分析了$http.jsonp發(fā)送跨站請求遇到的問題與相應(yīng)的解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03如何在Angular8.0下使用ngx-translate進(jìn)行國際化配置
這篇文章主要介紹了如何在Angular8.0下使用ngx-translate進(jìn)行國際化配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07AngularJS 實(shí)現(xiàn)購物車全選反選功能
這篇文章主要介紹了AngularJS 實(shí)現(xiàn)購物車全選反選功能,需要的朋友可以參考下2017-10-10