I tried what you are trying to do but was unsuccessful! :(
However I have couple of alternative for you.
Option 1:
Have an aura:attribute as currentValue and set the very first picklist value in it inside the setCallBack function. Use the variable wherever you need. Also, in the CHANGE event of inputSelect, assign the selected value to the currentValue.
In your component:
<aura:attribute name="currentValue" type="String"/>
<ui:inputSelect aura:id="P" change="{!c.setCurrentValue}">
...
</ui:inputSelect>
In your JS controller:
setCurrentValue: function(component, event, helper){
var newValue = component.find("P").get("v.value");
component.set("currentValue", newValue);
},
add : function(component, event, helper) {
var v = component.get("v.currentValue");
console.log('p..'+v);
}
In your helper class:
checkdefaultvalue:function(component, event, helper){
var action = component.get("c.temp");
action.setCallback(this, function(response){
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
var res = response.getReturnValue();
component.set("v.it", res);
component.set("v.currentValue", res[0]); //This will set the 1st value as current one
console.log("it optiom: "+res);
}else{console.log("failure...");}
});
I have tested this, its working fine.
Coming to Option 2:
inputSelect has an attribute 'updateOn' which takes an event name, firing which will update the selected Value of the picklist. By defult its CHANGE.
Now if you can fire the change event of inputSelect from JScontroller or helper, it should set the selected value of the inputSelect.
Your Component:
<ui:inputSelect aura:id="P" updateOn="change">
...
</ui:inputSelect>
In your JS Controller:
add : function(component, event, helper) {
component.find("P").getEvent("change").fire();
var defaultDropdown = component.find("P").get("v.value");
console.log('p..'+defaultDropdown);
}
I haven't tested the 2nd solution but theoretically it should work.
This has a similar solution to this issue:
How to remove items from a dynamic list in Lightning component?
It doesn't feel clean, but this will return both the value and the input that called it:
Component
<div class="slds-form-element__control">
<ui:inputPhone class="slds-input" placeholder="{!Phone}" change='c.save'>
</ui:inputPhone>
<span id="{!'thisInput'+i}"></span>
</div>
Controller
save: function(cmp, evt){
var targetCmp = evt.getSource();
var val = targetCmp.get('v.value');
var inputId = targetCmp.getElement().nextSibling.nextSibling.id;
console.log(value);
console.log(inputId);
}
If you're wondering why the double "nextSibling", it's because the ui:input renders something like:
<!--render facet: 0000:00;a-->
following the element.
I do hope there is a better way to do this.
Best Answer
As Tushar said you shouldn't pass any blank option value in the controller. Still if you want to set a dropdown value as default value in your Lightning component you can do this as below.
<aura:iteration
has a attribute indexVar which provides the position of the dropdown starting from 0. You can use this with<aura:if
to setvalue="true"
for any dropdown value to make it default. In your case it will be 0 as the first one is default. Hope this helps.Example
Your code will be updated as below