This repository was archived by the owner on May 29, 2019. It is now read-only.
fix(tabs): Initial tab selection #834
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
tab.active
setActive
only when alltab.active
are set up (on the nextdigest cycle)
Before I go to explain, there are up to two expressions that indicate
whether a tab is active:
active
variable in the isolate scope of the tab directiveattrs.active
) if it is set, I'll call thisgetActive
, as that's the variable that refers to it (setActive
assigns to it).During initial linking (adding of tabs), the
active
variable in the tab's isolate scope tracks the active tab. When the first tab is added, it'sactive
is set to true since there's no way to know if subsequent tabs are active since they haven't been added yet. As such, at this point, it is not meaningful to set assign thegetActive
with the value ofactive
. At least not until all the tabs have been added. Hence, a good time would be to wait until the next $digest cycle.A watcher is called asynchronously after initialization even if there is no change on the expression's value.
As such, we can leave that to the watcher for the
active
expression to initialize getActive by calling setActive during its initlization cycle.However, there is a chance (if the $digest cycles and planets align...) that the
active
variable gets initialized a second time using thegetActive
(in the watcher forgetActive
). Since we're already settingactive
togetActive
, and theactive
variable should now be carrying the truth. Avoid this re-initialization.This fix is ugly because the logic for the two-way binding of
active
and thegetActive
is complex. Consider refactoring the implementation for maintaining the two-way binding. It looks like this sort of binding is exactly what$compile
already does in compile.js#L973-L1001 . AngularJS 1.1.4 introduced the feature in angular/angular.js@ac899d0 to allow optional binding of variables in the isolate scope that handles this very cleanly just by declaringscope: {active: '=? active'}
for the directive.I didn't investigate into why the previous test with
ng-repeat
didn't expose this problem. It's possible it's because it had an additional digest cycle between linking such that the digest cycles didn't go in the order that would expose the problem.Fixes #747.