Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit d19b4c0

Browse files
bekospkozlowski-opensource
authored andcommitted
refactor(dropdown): remove isolated scope
Closes #1818
1 parent 378a933 commit d19b4c0

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

Diff for: src/dropdown/dropdown.js

+28-14
Original file line numberDiff line numberDiff line change
@@ -41,48 +41,62 @@ angular.module('ui.bootstrap.dropdown', [])
4141
};
4242
}])
4343

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;
4651

4752
this.init = function( element ) {
4853
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+
}
5063
};
5164

5265
this.toggle = function( open ) {
53-
return $scope.isOpen = arguments.length ? !!open : !$scope.isOpen;
66+
return scope.isOpen = arguments.length ? !!open : !scope.isOpen;
5467
};
5568

5669
// Allow other directives to watch status
5770
this.isOpen = function() {
58-
return $scope.isOpen;
71+
return scope.isOpen;
5972
};
6073

61-
$scope.$watch('isOpen', function( value ) {
74+
scope.$watch('isOpen', function( value ) {
6275
$animate[value ? 'addClass' : 'removeClass'](self.$element, openClass);
6376

6477
if ( value ) {
65-
dropdownService.open( $scope );
78+
dropdownService.open( scope );
6679
} else {
67-
dropdownService.close( $scope );
80+
dropdownService.close( scope );
6881
}
6982

70-
$scope.onToggle({ open: !!value });
83+
setIsOpen($scope, value);
84+
toggleInvoker($scope, { open: !!value });
7185
});
7286

7387
$scope.$on('$locationChangeSuccess', function() {
74-
$scope.isOpen = false;
88+
scope.isOpen = false;
89+
});
90+
91+
$scope.$on('$destroy', function() {
92+
scope.$destroy();
7593
});
7694
}])
7795

7896
.directive('dropdown', function() {
7997
return {
8098
restrict: 'CA',
8199
controller: 'DropdownController',
82-
scope: {
83-
isOpen: '=?',
84-
onToggle: '&'
85-
},
86100
link: function(scope, element, attrs, dropdownCtrl) {
87101
dropdownCtrl.init( element );
88102
}

0 commit comments

Comments
 (0)