2016-09-27 57 views
-1

我試圖在輸入文本框中的窗體中實現輸入清除圖標。我通過一個指令來做。但它沒有正常工作。每當我在輸入框中寫入東西時,十字按鈕不會顯示出來,除非它是有效的輸入,例如當我在公司名稱中鍵入三個字符時,只有它顯示十字按鈕,而不是當字符小於3或時該模式是錯誤的..我希望它應該顯示每當我在文本框中寫入任何值。當我點擊十字按鈕時,它應該清除文本,並且當我點擊十字時,應該顯示當前顯示的所需條件。我正在使用下面的代碼指令 -清除輸入圖標在正在驗證角度js的窗體中工作不正常

.directive('inputClearNoMaterial', function(){ 
    return { 
      restrict: 'A', 
      compile: function (element, attrs) { 
       var color = attrs.inputClearNoMaterial; 
       var style = color ? "color:" + color + ";" : ""; 
       var action = attrs.ngModel + " = ''"; 
       element.after(
        '<span class="animate-show"' + 
        'ng-show="' + attrs.ngModel + '" ng-click="' + action + '"' + 
        'style="position: absolute; margin: 3px 0px; cursor: pointer; ">' + 
        '<div style="' + style + '">x</div>' + 
        '</span>'); 
      } 
     }; 
     }); 

任何人都可以幫助我嗎?此外,交叉按鈕應該顯示在文本框內,目前在框外。我創建了一個plunker這裏 - https://plnkr.co/edit/4sXB3S9HAuvszlZdimIU?p=preview

+0

您可以檢查您plunkr鏈接修改您的指示的代碼,它不加載 –

+0

它的加載,但位慢慢地,請嘗試在firefox – Aanchal

+0

負面選民,也告訴我爲我的問題投票的原因,我認爲一切都從問題明確,如果你不能回答的問題,那麼也請不要-ve投票 – Aanchal

回答

1

這裏是工作link

按以下

.directive('inputClearNoMaterial', ['$compile','$timeout',function($compile,$timeout){ 
    return { 
    restrict: 'A', 
    require: 'ngModel', 
    scope: {}, 
    link: function(scope, el, attrs, ctrl) { 

    var color=attrs.inputClearNoMaterial; 
     var style = color ? "color:" + color + ";" : ""; 

     var template = $compile('<i ng-show="enabled" ng-mousedown="clear()" style="' + style + '" class="fa fa-times-circle"></i>')(scope); 
     el.after(template); 

     scope.clear = function() { 
     ctrl.$setViewValue(null); 
     ctrl.$render(); 
     $timeout(function() { 
      el[0].focus(); 
     }, 0, false); 
     }; 

     el.bind('input focus', function() { 
     scope.enabled = !ctrl.$isEmpty(el.val()); 
     scope.$apply(); 
     }) 
     .bind('blur', function() { 
     scope.enabled = false; 
     scope.$apply(); 
     }); 
    } 
    }; 
     }]); 
+0

在鏈接請提供檢查style.css文件也 –

+0

非常感謝,你的答案工作正常。如果我在輸入框的相同行中顯示錯誤信息,那麼您是否可以幫助我解決問題?當交叉符號出現並消失時,錯誤消息會波動。有沒有什麼辦法來修復這個十字按鈕的空間,使屏幕不會因十字按鈕而波動? @Balajivaishnav看到plunker-plnkr.co/edit/QYIZZqHS2FJAHF3eIaPj?p=preview - – Aanchal

+0

請張貼明確的鏈接 –