[SalesForce] Aura set (set the value in aura iteration)

I am trying to set the value of aura attribute in aura iteration.I am declaring

<aura:attribute name="flag" type="integer" default="0"/> 

And in <aura:iteraton> i am trying to set this flag value but i am unable to do it due to following error:

Failed to save undefined: The attribute "flag" was not found on the COMPONENT markup://aura:iteration: Source

And i am setting the value like following:

<aura:attribute name="flag" value="0" type="integer"/>
  <aura:iteration>
  <!-- some condition goes here to set the value-->
<aura:set attribute="flag" value="{add(v.flag,1)}"/>
</aura:iteration>

Best Answer

aura:set doesn't work like this.

From the doc:

When you include another component, such as , in a component, we call that a component reference to . You can use to set an attribute on the component reference.

aura:set when used inside a parent element, it sets the value of the attribute of the parent element. Here, your parent element is aura:iteration, so its searching for an attribute on aura:iteration which is obviously not present.

The error you are getting is explaining the same:

Failed to save undefined: The attribute "flag" was not found on the COMPONENT markup://aura:iteration: Source