0

All:

I have a function that is activated by an ng-click event. I want this function to wait on another ng-click event. The idea is to choose an action, and then choose a target for that action (via clickable objects in the DOM).

I'm not sure how to accomplish this. I know that I can use an Angular deferred event (using Q). However, how can I capture an ng-click event inside my original function without having to hold a reference to the deferred object within my scope?

Edit: The reason I'd like to design it this way is because I don't want my "targets" to perform any action on ng-click unless an action has been clicked; targets should only be selectable after an action has been selected.

1 Answer 1

3

If I understand your problem correctly, you can use the controller's $scope to keep track of this state information. E.g., in the ng-click function for an action, set a $scope.action property. In the ng-click function for the target, check that an appropriate $scope.action has been set.

If you have multiple targets, and you need to keep track an action for each one, use an array to keep track of which actions have been selected.

Sign up to request clarification or add additional context in comments.

2 Comments

Is this the best way to do this? Holding state info in the scope (in this case) is analogous to using a global variable to hold state info between functions. I have three characters that can perform actions on any of six targets; each character holds an array of actions to perform. If at all possible, I'd like to keep everything local to my functions and wait for user input after an action is selected for a specific character. In the long run, this would be simpler to extend.
I don't think it is as bad as a global variable -- it is limited/scoped to the current view/controller. You could instead store the selected action on your character models, or you could store the data right on your function objects. I really can't say if any of these are the "best" way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.