2016-07-02 20 views
7

ho usato uno dello stile da qui: http://tympanus.net/Development/TextInputEffects/index.htmletichetta di ingresso flottante angolare da utilizzare typeahead

Per creare una direttiva di ingresso, vedere plunker: https://plnkr.co/edit/wELJGgUUoiykcp402u1G?p=preview

Questo lavoro grande per i campi di input standard, tuttavia, sto faticando a lavorare su Twitter typeahead: https://github.com/twitter/typeahead.js/

Domanda - Come posso usare la mia etichetta di input floating con un typeahead?

app.directive('floatInput', function($compile) { 
    return { 
    restrict: 'E', 
    replace: true, 
    transclude: true, 
    scope: { 
     elemTitle: '=elemTitle', 
     elemtId: '=elemeId' 
    }, 
    templateUrl: 'input-template.html', 
    link: function(scope, elem, attrs) { 
     var ngModelName = elem.attr('input-model'); 
     var inputElem = angular.element(elem[0].querySelector('input')); 
     inputElem.attr('ng-model', ngModelName); 

     $compile(inputElem)(scope); 
     $compile(inputElem)(scope.$parent); 

     var inputLabel = angular.element(elem[0].querySelector('label span')); 
     inputLabel.attr('ng-class', '{\'annimate-input\' : '+ngModelName+'.length > 0}'); 
     $compile(inputLabel)(scope); 
    }, 
    controller: function($scope) { 
     $scope.title = $scope.elemTitle; 
     $scope.inputId = $scope.elemId 
    } 
    } 
}) 

HTML:

<div> 
<span class="input input--juro"> 
    <input class="input__field input__field--juro" type="text" id="{{inputId}}" ng-model="tmp" /> 
    <label class="input__label input__label--juro" for="{{inputId}}"> 
    <span class="input__label-content input__label-content--juro">{{title}}</span> 
    </label> 
    </span> 
</div> 
+0

in cui è stato fatto riferimento 'typeahead' nel codice? –

+0

@JossefHarush: non ho idea di dove cominciare. –

+0

Si prega di essere più chiaro su quale domanda avete. Che cosa hai provato? Vedi un errore? –

risposta

4

Il modo più semplice che conosco per raggiungere questo obiettivo è per inizializzare l'ingresso typeahead nella funzione link della direttiva. Per inizializzare il typeahead con le opzioni disponibili creerei un parametro facoltativo per la direttiva e inizializzerò selettivamente l'input come un input typeahead se l'elenco è fornito.

Ecco un esempio di come la direttiva potrebbe apparire invece:

app.directive('floatInput', function($compile) { 
    return { 
    restrict: 'E', 
    replace: true, 
    transclude: true, 
    scope: { 
     elemTitle: '=elemTitle', 
     elemtId: '=elemeId', 
     typeaheadSrc: '=?typeaheadSrc' 
    }, 
    templateUrl: 'input-template.html', 
    link: function(scope, elem, attrs) { 
     var inputElem = angular.element(elem[0].querySelector('input')); 

     if(scope.typeaheadSrc && scope.typeaheadSrc.length > 0){ 
     var typeahead = jQuery(inputElem).typeahead({ 
      hint: true, 
      highlight: true, 
      minLength: 1 
     }, { 
      name: 'typeahead', 
      source: substringMatcher(scope.typeaheadSrc) 
     }); 
     } 
    }, 
    controller: function($scope) { 
     $scope.title = $scope.elemTitle; 
     $scope.inputId = $scope.elemId 
    } 
    } 
}); 
// from http://twitter.github.io/typeahead.js/examples/ 
var substringMatcher = function(strs) { 
    return function findMatches(q, cb) { 
    var matches= [], 
     substrRegex = new RegExp(q, 'i'); 

    $.each(strs, function(i, str) { 
     if (substrRegex.test(str)) { 
     matches.push({value: str}); 
     } 
    }); 

    cb(matches); 
    }; 
}; 

Ho aggiornato il vostro plunker per ottenere il risultato desiderato: Plunker

Problemi correlati