@@ -41,48 +41,62 @@ angular.module('ui.bootstrap.dropdown', [])
41
41
} ;
42
42
} ] )
43
43
44
- . controller ( 'DropdownController' , [ '$scope' , '$attrs' , 'dropdownConfig' , 'dropdownService' , '$animate' , function ( $scope , $attrs , dropdownConfig , dropdownService , $animate ) {
45
- var self = this , openClass = dropdownConfig . openClass ;
44
+ . controller ( 'DropdownController' , [ '$scope' , '$attrs' , '$parse' , 'dropdownConfig' , 'dropdownService' , '$animate' , function ( $scope , $attrs , $parse , dropdownConfig , dropdownService , $animate ) {
45
+ var self = this ,
46
+ scope = $scope . $new ( ) , // create a child scope so we are not polluting original one
47
+ openClass = dropdownConfig . openClass ,
48
+ getIsOpen ,
49
+ setIsOpen = angular . noop ,
50
+ toggleInvoker = $attrs . onToggle ? $parse ( $attrs . onToggle ) : angular . noop ;
46
51
47
52
this . init = function ( element ) {
48
53
self . $element = element ;
49
- $scope . isOpen = angular . isDefined ( $attrs . isOpen ) ? $scope . $parent . $eval ( $attrs . isOpen ) : false ;
54
+
55
+ if ( $attrs . isOpen ) {
56
+ getIsOpen = $parse ( $attrs . isOpen ) ;
57
+ setIsOpen = getIsOpen . assign ;
58
+
59
+ $scope . $watch ( getIsOpen , function ( value ) {
60
+ scope . isOpen = ! ! value ;
61
+ } ) ;
62
+ }
50
63
} ;
51
64
52
65
this . toggle = function ( open ) {
53
- return $ scope. isOpen = arguments . length ? ! ! open : ! $ scope. isOpen ;
66
+ return scope . isOpen = arguments . length ? ! ! open : ! scope . isOpen ;
54
67
} ;
55
68
56
69
// Allow other directives to watch status
57
70
this . isOpen = function ( ) {
58
- return $ scope. isOpen ;
71
+ return scope . isOpen ;
59
72
} ;
60
73
61
- $ scope. $watch ( 'isOpen' , function ( value ) {
74
+ scope . $watch ( 'isOpen' , function ( value ) {
62
75
$animate [ value ? 'addClass' : 'removeClass' ] ( self . $element , openClass ) ;
63
76
64
77
if ( value ) {
65
- dropdownService . open ( $ scope ) ;
78
+ dropdownService . open ( scope ) ;
66
79
} else {
67
- dropdownService . close ( $ scope ) ;
80
+ dropdownService . close ( scope ) ;
68
81
}
69
82
70
- $scope . onToggle ( { open : ! ! value } ) ;
83
+ setIsOpen ( $scope , value ) ;
84
+ toggleInvoker ( $scope , { open : ! ! value } ) ;
71
85
} ) ;
72
86
73
87
$scope . $on ( '$locationChangeSuccess' , function ( ) {
74
- $scope . isOpen = false ;
88
+ scope . isOpen = false ;
89
+ } ) ;
90
+
91
+ $scope . $on ( '$destroy' , function ( ) {
92
+ scope . $destroy ( ) ;
75
93
} ) ;
76
94
} ] )
77
95
78
96
. directive ( 'dropdown' , function ( ) {
79
97
return {
80
98
restrict : 'CA' ,
81
99
controller : 'DropdownController' ,
82
- scope : {
83
- isOpen : '=?' ,
84
- onToggle : '&'
85
- } ,
86
100
link : function ( scope , element , attrs , dropdownCtrl ) {
87
101
dropdownCtrl . init ( element ) ;
88
102
}
0 commit comments