@@ -13,89 +13,95 @@ angular.module('ionic.ui.tabs', ['ionic.service.view', 'ionic.ui.bindHtml'])
13
13
$ionicViewService . disableRegisterByTagName ( 'tabs' ) ;
14
14
} ] )
15
15
16
- . directive ( 'tabs' , [ '$ionicViewService' , function ( $ionicViewService ) {
17
- return {
18
- restrict : 'E' ,
19
- replace : true ,
20
- scope : true ,
21
- transclude : true ,
22
- controller : [ '$scope' , '$element' , function ( $scope , $element ) {
23
- var _this = this ;
24
-
25
- $scope . tabCount = 0 ;
26
- $scope . selectedIndex = - 1 ;
27
- $scope . $enableViewRegister = false ;
28
-
29
- angular . extend ( this , ionic . controllers . TabBarController . prototype ) ;
30
-
31
-
32
- ionic . controllers . TabBarController . call ( this , {
33
- controllerChanged : function ( oldC , oldI , newC , newI ) {
34
- $scope . controllerChanged && $scope . controllerChanged ( {
35
- oldController : oldC ,
36
- oldIndex : oldI ,
37
- newController : newC ,
38
- newIndex : newI
39
- } ) ;
40
- } ,
41
- tabBar : {
42
- tryTabSelect : function ( ) { } ,
43
- setSelectedItem : function ( index ) { } ,
44
- addItem : function ( item ) { }
45
- }
16
+ . controller ( '$ionicTabs' , [ '$scope' , '$ionicViewService' , function ( $scope , $ionicViewService ) {
17
+ var _this = this ;
18
+
19
+ $scope . tabCount = 0 ;
20
+ $scope . selectedIndex = - 1 ;
21
+ $scope . $enableViewRegister = false ;
22
+
23
+ angular . extend ( this , ionic . controllers . TabBarController . prototype ) ;
24
+
25
+ ionic . controllers . TabBarController . call ( this , {
26
+ controllerChanged : function ( oldC , oldI , newC , newI ) {
27
+ $scope . controllerChanged && $scope . controllerChanged ( {
28
+ oldController : oldC ,
29
+ oldIndex : oldI ,
30
+ newController : newC ,
31
+ newIndex : newI
46
32
} ) ;
33
+ } ,
34
+ tabBar : {
35
+ tryTabSelect : function ( ) { } ,
36
+ setSelectedItem : function ( index ) { } ,
37
+ addItem : function ( item ) { }
38
+ }
39
+ } ) ;
47
40
48
- this . add = function ( tabScope ) {
49
- tabScope . tabIndex = $scope . tabCount ;
50
- this . addController ( tabScope ) ;
51
- if ( tabScope . tabIndex === 0 ) {
52
- this . select ( 0 ) ;
53
- }
54
- $scope . tabCount ++ ;
55
- } ;
41
+ this . add = function ( tabScope ) {
42
+ tabScope . tabIndex = $scope . tabCount ;
43
+ this . addController ( tabScope ) ;
44
+ if ( tabScope . tabIndex === 0 ) {
45
+ this . select ( 0 ) ;
46
+ }
47
+ $scope . tabCount ++ ;
48
+ } ;
56
49
57
- this . select = function ( tabIndex , emitChange ) {
58
- if ( tabIndex !== $scope . selectedIndex ) {
59
-
60
- $scope . selectedIndex = tabIndex ;
61
- $scope . activeAnimation = $scope . animation ;
62
- _this . selectController ( tabIndex ) ;
63
-
64
- var viewData = {
65
- type : 'tab' ,
66
- typeIndex : tabIndex
67
- } ;
68
-
69
- for ( var x = 0 ; x < this . controllers . length ; x ++ ) {
70
- if ( tabIndex === this . controllers [ x ] . tabIndex ) {
71
- viewData . title = this . controllers [ x ] . title ;
72
- viewData . historyId = this . controllers [ x ] . $historyId ;
73
- viewData . url = this . controllers [ x ] . url ;
74
- viewData . uiSref = this . controllers [ x ] . viewSref ;
75
- viewData . navViewName = this . controllers [ x ] . navViewName ;
76
- viewData . hasNavView = this . controllers [ x ] . hasNavView ;
77
- break ;
78
- }
79
- }
80
- if ( emitChange ) {
81
- $scope . $emit ( 'viewState.changeHistory' , viewData ) ;
82
- }
83
- } else if ( emitChange ) {
84
- var currentView = $ionicViewService . getCurrentView ( ) ;
85
- if ( currentView ) {
86
- $ionicViewService . goToHistoryRoot ( currentView . historyId ) ;
87
- }
88
- }
50
+ function controllerByTabIndex ( tabIndex ) {
51
+ for ( var x = 0 ; x < _this . controllers . length ; x ++ ) {
52
+ if ( _this . controllers [ x ] . tabIndex === tabIndex ) {
53
+ return _this . controllers [ x ] ;
54
+ }
55
+ }
56
+ }
57
+
58
+ this . select = function ( tabIndex , emitChange ) {
59
+ if ( tabIndex !== $scope . selectedIndex ) {
60
+
61
+ $scope . selectedIndex = tabIndex ;
62
+ $scope . activeAnimation = $scope . animation ;
63
+ _this . selectController ( tabIndex ) ;
64
+
65
+ var viewData = {
66
+ type : 'tab' ,
67
+ typeIndex : tabIndex
89
68
} ;
90
69
91
- $scope . controllers = this . controllers ;
70
+ var tabController = controllerByTabIndex ( tabIndex ) ;
71
+ if ( tabController ) {
72
+ viewData . title = tabController . title ;
73
+ viewData . historyId = tabController . $historyId ;
74
+ viewData . url = tabController . url ;
75
+ viewData . uiSref = tabController . viewSref ;
76
+ viewData . navViewName = tabController . navViewName ;
77
+ viewData . hasNavView = tabController . hasNavView ;
78
+ }
92
79
93
- $scope . tabsController = this ;
80
+ if ( emitChange ) {
81
+ $scope . $emit ( 'viewState.changeHistory' , viewData ) ;
82
+ }
83
+ } else if ( emitChange ) {
84
+ var currentView = $ionicViewService . getCurrentView ( ) ;
85
+ if ( currentView ) {
86
+ $ionicViewService . goToHistoryRoot ( currentView . historyId ) ;
87
+ }
88
+ }
89
+ } ;
94
90
95
- } ] ,
91
+ $scope . controllers = this . controllers ;
96
92
97
- template : '<div class="view"><tab-controller-bar></tab-controller-bar></div>' ,
93
+ $scope . tabsController = this ;
94
+
95
+ } ] )
98
96
97
+ . directive ( 'tabs' , [ '$ionicViewService' , function ( $ionicViewService ) {
98
+ return {
99
+ restrict : 'E' ,
100
+ replace : true ,
101
+ scope : true ,
102
+ transclude : true ,
103
+ controller : '$ionicTabs' ,
104
+ template : '<div class="view"><tab-controller-bar></tab-controller-bar></div>' ,
99
105
compile : function ( element , attr , transclude , tabsCtrl ) {
100
106
return function link ( $scope , $element , $attr ) {
101
107
@@ -173,9 +179,9 @@ angular.module('ionic.ui.tabs', ['ionic.service.view', 'ionic.ui.bindHtml'])
173
179
$scope . $watch ( badgeGet , function ( value ) {
174
180
$scope . badge = value ;
175
181
} ) ;
176
- var badgeStyleGet = $interpolate ( attr . badgeStyle || '' ) ;
177
- $scope . $watch ( badgeStyleGet , function ( val ) {
178
- $scope . badgeStyle = val ;
182
+
183
+ $attr . $observe ( 'badgeStyle' , function ( value ) {
184
+ $scope . badgeStyle = value ;
179
185
} ) ;
180
186
181
187
var leftButtonsGet = $parse ( $attr . leftButtons ) ;
@@ -193,25 +199,31 @@ angular.module('ionic.ui.tabs', ['ionic.service.view', 'ionic.ui.bindHtml'])
193
199
194
200
tabsCtrl . add ( $scope ) ;
195
201
196
- $scope . $watch ( 'isVisible' , function ( value ) {
202
+ function cleanupChild ( ) {
197
203
if ( childElement ) {
198
204
childElement . remove ( ) ;
199
205
childElement = null ;
200
- $rootScope . $broadcast ( 'tab.hidden' ) ;
201
206
}
202
207
if ( childScope ) {
203
208
childScope . $destroy ( ) ;
204
209
childScope = null ;
205
210
}
206
- if ( value ) {
211
+ }
212
+
213
+ $scope . $watch ( 'isVisible' , function ( value ) {
214
+ if ( value ) {
215
+ cleanupChild ( ) ;
207
216
childScope = $scope . $new ( ) ;
208
217
transclude ( childScope , function ( clone ) {
209
218
clone . addClass ( 'pane' ) ;
210
219
clone . removeAttr ( 'title' ) ;
211
220
childElement = clone ;
212
221
$element . parent ( ) . append ( childElement ) ;
213
222
} ) ;
214
- $rootScope . $broadcast ( 'tab.shown' ) ;
223
+ $scope . $broadcast ( 'tab.shown' ) ;
224
+ } else if ( childScope ) {
225
+ $scope . $broadcast ( 'tab.hidden' ) ;
226
+ cleanupChild ( ) ;
215
227
}
216
228
} ) ;
217
229
@@ -229,13 +241,15 @@ angular.module('ionic.ui.tabs', ['ionic.service.view', 'ionic.ui.bindHtml'])
229
241
}
230
242
} ) ;
231
243
232
- $rootScope . $on ( '$stateChangeSuccess' , function ( value ) {
244
+ var unregister = $rootScope . $on ( '$stateChangeSuccess' , function ( value ) {
233
245
if ( $ionicViewService . isCurrentStateNavView ( $scope . navViewName ) &&
234
246
$scope . tabIndex !== tabsCtrl . selectedIndex ) {
235
247
tabsCtrl . select ( $scope . tabIndex ) ;
236
248
}
237
249
} ) ;
238
250
251
+ $scope . $on ( '$destroy' , unregister ) ;
252
+
239
253
} ;
240
254
}
241
255
} ;
0 commit comments