user3384672 user3384672 - 2 months ago 22
AngularJS Question

set uib-typeahead attribute Dynamically

I have a Directive which wraps uib-typeahead, the thing is that I want to populate the uib-typeahead with different arrays of objects which means different names for the keys of the objects inside the arrays I have this

My directive which wraps uib typeahead

<input-text-material-typeahead input-model="rendimiento.operador" label-model="textType" holder="Destino" color="#5E35B1" lista="Operadores" label="Destino"></input-text-material-typeahead>


And this are the guts

ng.app.directive('inputTextMaterialTypeahead', function() {
return{
restrict:'EA',
scope:{
holder:'@',
color:'@',
lista:'@',
item:'@',
inputModel:'=',
method:'@'
},
controller:function($scope, $listas, $element){
$scope.getItem = function(val){
return $listas[$scope.lista](val, function(response){
return response.map(function(item) {
return item;
});
});
}

$scope.onSelect = function($item, $model, $label){
//
$scope.inputModel = $item;
}
},
link:function(scope, element, attrs, color){
//
var input_text = element.find('#input-text');
var label_rendimiento = element.find('#label-rendimiento');

input_text.on('focus', function(){
label_rendimiento.addClass('show');
label_rendimiento.css({'color':scope.color});
element.find('.input-rendimiento').css({'border-bottom':'solid 3px '+scope.color+''});
input_text.attr('placeholder', '');
});

input_text.on('blur', function(){
if(!input_text.val()){
label_rendimiento.removeClass('show');
input_text.attr('placeholder', scope.holder);
element.find('.input-rendimiento').css({'border-bottom': 'solid 1px #BDBDBD'});
}
else{
label_rendimiento.css({'color':'#BDBDBD'});
element.find('.input-rendimiento').css({'border-bottom': 'solid 1px #BDBDBD'});
}
});
},
templateUrl:ng.components + '/input-typeahead-material.html'
}


});

the HTML of the directive

<div class="col-xs-12 nopad">
<label class="label-rendimiento" id="label-rendimiento">{{holder}}</label>
</div>
<div class="col-xs-12 nopad input-rendimiento">
<input ng-model="whatever" placeholder="Unidad" id="input-text" uib-typeahead="Clave as Clave.Nombre for Clave in getItem($viewValue)" typeahead-loading="loadingLocations" ng-model-options="{debounce:800}" typeahead-on-select="onSelect($item, $model, $label)"/>

<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>


Now Instead of this

uib-typeahead="Clave as Clave.Nombre for Clave in getItem($viewValue)"


I want this

uib-typeahead="{{expression}} in getItem($viewValue)"


and set in the directive link the expression i.e.

scope.expression = 'Clave as Clave.Nombre for Clave';


Please help, thanks.

Answer

It was like this

uib-typeahead="Label as Label[key] for Label in getItem($viewValue)"

and in the directivedeclaration I just add 'key' to the scope

 scope:{
    holder:'@',
    color:'@',
    lista:'@',
    item:'@',
    inputModel:'=',
    key:'@'
},

and when the directive is called is like this

<input-text-material-typeahead  key="Operador" input-model="rendimiento.operador" label-model="textType" holder="Destino" color="#5E35B1" lista="Operadores" label="Destino"></input-text-material-typeahead>
Comments