angular.js - angular.ui:dropdown的下拉菜單關(guān)閉的偶爾失常問(wèn)題
問(wèn)題描述
本人遇到一個(gè)很奇怪的問(wèn)題,點(diǎn)擊菜單中的選擇項(xiàng),大多數(shù)情況下菜單隱藏正常,小部分情況就出現(xiàn)了如上圖情況。(我檢查過(guò)dropdown的is-open值,的確已經(jīng)為false。)不知道問(wèn)題出在哪里,請(qǐng)高手指教,謝謝。
以下為我的部分代碼
.directive(’pxSearchInputGroup’,function($ListCache,$filter,$timeout,$rootScope,$currentUser,$parse){ return {restrict:’EA’,scope:{ assortmentList:’=’, isOpen:’=’, inputPlaceholder:’@’, templateType:’@’, afterSelect:’&’},templateUrl:’../templates/widget/search-input-list.html’,controller:function($scope,$attrs){ var self=this,assortmentList,teamAssortment,userAssortment,teamList,userList; //var getIsOpen=$parse($attrs.isOpen); //var setIsOpen=getIsOpen.assign||angular.noop; this.init=function(){assortmentList=angular.copy($scope.assortmentList);teamAssortment= _.findWhere(assortmentList,{type:’team’});userAssortment= _.findWhere(assortmentList,{type:’user’});$scope.vm={ key:’’, isOpen:$scope.isOpen};$scope.currentUser=$currentUser.get();$scope.$watch(’vm.key’,function(newKey,oldKey){ var condition={$or:{ nickName:{$likeI:newKey }, email:{$likeI:newKey }, mark:{$likeI:newKey }, name:{$likeI:newKey }} }; if(newKey!==oldKey){if(newKey===’’||!newKey){ condition=''; $scope.isSelect=false;}if(!$scope.isSelect){ $scope.selectedItem=’’; self.doSearch(condition);}$scope.isSelect=false; }});//空白區(qū)域點(diǎn)擊關(guān)閉下拉菜單$scope.$on(’$changeDropMenuState’,function(e,isOpen){ $scope.vm.isOpen=isOpen;}); }; this.searchContacts=function(condition){userList=userAssortment?userAssortment.list:null;return condition &&userList? userList.$query(condition): userList; }; this.searchTeams=function(condition){teamList=teamAssortment?teamAssortment.list:null;return condition &&teamList? teamList.$query(condition): teamList; }; this.selectItem=function(item,type){$scope.isSelect=true;$scope.vm.selectedItem=item;$scope.vm.key=type==’user’ ? $filter(’pxUserName’)(item) :item.name;this.toggleDropMenu(false);if(angular.isFunction($scope.afterSelect)){ $scope.afterSelect({item:item});} }; this.doSearch=function(condition){angular.forEach($scope.assortmentList,function(assortment){ assortment.list=self.getSearchResult(condition,assortment.type);}); }; this.getSearchResult=function(condition,type){if(type==’user’){ return this.searchContacts(condition);}else{ return this.searchTeams(condition);} }; this.toggleDropMenu=function(isOpen){if($scope.templateType===’chat’){ return;}$scope.vm.isOpen=isOpen;$scope.isOpen=isOpen;//setIsOpen($scope,isOpen); };},link:function(scope,element,attr,ctrl){ ctrl.init(); scope.onItemClick=function(item,type){ctrl.selectItem(item,type); }; //此處的input有dropdown-toggle角色,當(dāng)下拉菜單處于關(guān)閉狀態(tài)時(shí),解決input輸入文字不顯示下拉菜單的問(wèn)題 scope.openDropMenu=function(){$timeout(function(){ ctrl.toggleDropMenu(true);},0); };} };}).directive(’pxSearchInput’,function(){ return {restrict:’EA’,replace:true,template:’<input placeholder='' ng-model='vm.key' stop-propagation-element type='text' ng-click='openDropMenu()' ng-keydown='openDropMenu()' />’,require:[’?^pxSearchInputGroup’,’^?ngModel’],link:function(scope,element,attr,ctrl){} };}).directive(’pxAssortmentList’,function(){ return {restrict:’EA’,templateUrl:’../templates/widget/search-mode.html’,replace:true,link:function($scope,element,attr){} };})
//外部數(shù)據(jù)scope.shareConfig={ isOpen:false, searchKey:’’, isSelected:false, assortmentList:[{title:’群組’,list:scope.teams,type:’team’},{title:’對(duì)象’,list:scope.contactAndMemberList,type:’user’,iconSize:’mini’} ], typeName:’任務(wù)’};scope.closePopup=function(){ scope.shareConfig.isOpen=false; $rootScope.$broadcast(’$changeDropMenuState’,false);};
//引用的地方<p ng-click='closePopup($event)'> <h3 ng-bind='::shareItem.title||shareItem.name||shareItem.docName'></h3> <form name='shareForm' ng-submit='onSubmit($event)'><span class='px-label-name'>發(fā)送到</span> <px-search-input-group input-placeHolder='選擇當(dāng)前團(tuán)隊(duì)群、聯(lián)系人' assortment-list='shareConfig.assortmentList' is-open='shareConfig.isOpen' after-select='afterSelect(item)'></px-search-input-group><p class='explain'>將{{::shareConfig.typeName}}轉(zhuǎn)發(fā)給當(dāng)前團(tuán)隊(duì)群、聯(lián)系人。</p><p class='px-dialog-footer'> <button ng-disabled='!selectedItem'>轉(zhuǎn)發(fā)</button></p> </form></p>
//search-input-list.html<p ng-switch='templateType'> <p ng-switch-when='chat'><p class='col-sm-12'> <input px-search-input placeholder='{{inputPlaceholder}}' /></p><px-assortment-list ng-repeat='assortment in assortmentList'></px-assortment-list> </p> <p ng-switch-default><p dropdown is-open='vm.isOpen'> <input px-search-input dropdown-toggle placeholder='{{inputPlaceholder}}' /> <p role='menu' style='max-height:300px;width:100%;overflow: auto;'><px-assortment-list ng-repeat='assortment in assortmentList'></px-assortment-list> </p></p> </p></p>
//search-mode.html<p > <h6 ng-bind='::assortment.title'></h6> <p ng-if='assortment.list.length==0' ng-bind='::’沒(méi)有找到’+assortment.title' style='padding-left:20px;'></p> <ul ng-if='assortment.list.length>0' ng-class='{’px-user-list’:assortment.type==’user’}'><li ng-repeat='item in assortment.list' ng-click='onItemClick(item,assortment.type)' ng-if='item.targetUid!=currentUser.id'> <px-user-photo ng-if='assortment.type==’user’' user='item.profile||item' size='{{assortment.iconSize}}' show-status='{{assortment.showState}}'></px-user-photo> <span ng-if='assortment.type==’file’' class='file-{{assortment.iconSize}}-{{item.suffix}}'></span> <span ng-if='assortment.type==’calender’' class='px-icon icon-calendar'></span> <span ng-if='assortment.type==’task’' class='px-icon icon-task'></span> <a href='javascript:;' ng-bind='item.name' ng-if='assortment.type!=’user’'></a> <a href='javascript:;' ng-bind='::item|pxUserName' ng-if='assortment.type==’user’'></a></li> </ul></p>
問(wèn)題解答
回答1:在 $scope.vm.isOpen=isOpen;外圍套一個(gè)
$timeout(function(){ $scope.vm.isOpen=isOpen; })
看看
相關(guān)文章:
1. node.js - gulp文件監(jiān)聽(tīng)的問(wèn)題2. node.js - node 客戶(hù)端socket一直報(bào)錯(cuò)Error: read ECONNRESET,用php的socket沒(méi)問(wèn)題哈。。3. python - 模擬滑動(dòng)驗(yàn)證碼,有源碼,求解4. npm鏡像站全新上線5. mySql排序,序號(hào)6. tp6表單令牌7. 老哥們求助啊8. javascript - vue-router怎么不能實(shí)現(xiàn)跳轉(zhuǎn)呢9. html5 - angularjs中外部模版加載無(wú)法使用10. css3 - 請(qǐng)問(wèn)一下在移動(dòng)端CSS布局布局中通常需要用到哪些元素,屬性?
