First, let me explain the effect I am looking for:
Layout:
- Header at the top of the page that says "Choose between...".
- Two input boxes below, input1 and input2
Effect
- If the user begins typing first in [choice1] in input1, the header auto-updates and says "Choose between [choice 1] and..."
- If the user begins typing first in [choice2] in input2, the header auto-updates and says "Choose between ___ and [choice2]"
- If the user has already written in one input and begins writing in the other input, the header auto-updates and says "Choose between [choice1] and [choice2]".
Now, I have already written the angular-js code to achieve this. I am not looking for an answer on how to achieve the effect. Instead, I want to know the logic for the best way to achieve this effect because I think the way I did it could be improved.
Finally, here is the logic I used:
- Create 4 divs with 4 ng-switch-when properties. Each div contains a header. First div contains header with "Choose between..." written on it. Second div contains header with "Choose between [input1text] and..." written on it. Third div contains a header with "Choose between ___ and [input2text]" written on it. Final div has "Choose between [input1text] and [input2text]" written on it. Each div has an ng-switch-when property, they are: "not-pressed", "first-pressed", "second-pressed", and "both-pressed", respectively
- If someone starts typing in input1, I see if either second-pressed state or both-pressed state is currently active. If one is, I set the state to both-pressed. Otherwise, I set the state to first-pressed.
- If someone starts typing in input2, I see if either first-pressed state or both-pressed state is currently active. If one is, I set the state to both-pressed. Otherwise, I set the state to second-pressed.
I think the way I wrote it is really clunky and messy and I'd like an opinion on if there is a better way to apprach the logic to this feature. Again, I am not looking for code. I just want to see if there is better logic.