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

refactor(dropdown): remove isolated scope #1818

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions src/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,62 @@ angular.module('ui.bootstrap.dropdown', [])
};
}])

.controller('DropdownController', ['$scope', '$attrs', 'dropdownConfig', 'dropdownService', '$animate', function($scope, $attrs, dropdownConfig, dropdownService, $animate) {
var self = this, openClass = dropdownConfig.openClass;
.controller('DropdownController', ['$scope', '$attrs', '$parse', 'dropdownConfig', 'dropdownService', '$animate', function($scope, $attrs, $parse, dropdownConfig, dropdownService, $animate) {
var self = this,
scope = $scope.$new(), // create a child scope so we are not polluting original one
openClass = dropdownConfig.openClass,
getIsOpen,
setIsOpen = angular.noop,
toggleInvoker = $attrs.onToggle ? $parse($attrs.onToggle) : angular.noop;

this.init = function( element ) {
self.$element = element;
$scope.isOpen = angular.isDefined($attrs.isOpen) ? $scope.$parent.$eval($attrs.isOpen) : false;

if ( $attrs.isOpen ) {
getIsOpen = $parse($attrs.isOpen);
setIsOpen = getIsOpen.assign;

$scope.$watch(getIsOpen, function(value) {
scope.isOpen = !!value;
});
}
};

this.toggle = function( open ) {
return $scope.isOpen = arguments.length ? !!open : !$scope.isOpen;
return scope.isOpen = arguments.length ? !!open : !scope.isOpen;
};

// Allow other directives to watch status
this.isOpen = function() {
return $scope.isOpen;
return scope.isOpen;
};

$scope.$watch('isOpen', function( value ) {
scope.$watch('isOpen', function( value ) {
$animate[value ? 'addClass' : 'removeClass'](self.$element, openClass);

if ( value ) {
dropdownService.open( $scope );
dropdownService.open( scope );
} else {
dropdownService.close( $scope );
dropdownService.close( scope );
}

$scope.onToggle({ open: !!value });
setIsOpen($scope, value);
toggleInvoker($scope, { open: !!value });
});

$scope.$on('$locationChangeSuccess', function() {
$scope.isOpen = false;
scope.isOpen = false;
});

$scope.$on('$destroy', function() {
scope.$destroy();
});
}])

.directive('dropdown', function() {
return {
restrict: 'CA',
controller: 'DropdownController',
scope: {
isOpen: '=?',
onToggle: '&'
},
link: function(scope, element, attrs, dropdownCtrl) {
dropdownCtrl.init( element );
}
Expand Down