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

Commit

Permalink
feat(buttons): add controllerAs support
Browse files Browse the repository at this point in the history
- Expose controller to the view for `btnCheckbox` and `btnRadio`
  directives

Closes #4140
  • Loading branch information
wesleycho committed Aug 7, 2015
1 parent 88a885c commit 02872dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/buttons/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ angular.module('ui.bootstrap.buttons', [])
return {
require: ['btnRadio', 'ngModel'],
controller: 'ButtonsController',
controllerAs: 'buttons',
link: function (scope, element, attrs, ctrls) {
var buttonsCtrl = ctrls[0], ngModelCtrl = ctrls[1];

Expand Down Expand Up @@ -45,6 +46,7 @@ angular.module('ui.bootstrap.buttons', [])
return {
require: ['btnCheckbox', 'ngModel'],
controller: 'ButtonsController',
controllerAs: 'button',
link: function (scope, element, attrs, ctrls) {
var buttonsCtrl = ctrls[0], ngModelCtrl = ctrls[1];

Expand Down
22 changes: 22 additions & 0 deletions src/buttons/test/buttons.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ describe('buttons', function () {
return el;
};

it('should expose the controller to the view', inject(function ($templateCache) {
var btn = compileButton('<button ng-model="model" btn-checkbox>{{button.text}}</button>', $scope);
var ctrl = btn.controller('btnCheckbox');
expect(ctrl).toBeDefined();

ctrl.text = 'foo';
$scope.$digest();

expect(btn.html()).toBe('foo');
}));

//model -> UI
it('should work correctly with default model values', function () {
$scope.model = false;
Expand Down Expand Up @@ -135,6 +146,17 @@ describe('buttons', function () {
return el.find('button');
};

it('should expose the controller to the view', inject(function ($templateCache) {
var btn = compileButtons('<button ng-model="model" btn-radio="1">{{buttons.text}}</button>', $scope);
var ctrl = btn.controller('btnRadio');
expect(ctrl).toBeDefined();

ctrl.text = 'foo';
$scope.$digest();

expect(btn.html()).toBe('foo');
}));

//model -> UI
it('should work correctly set active class based on model', function () {
var btns = compileButtons('<button ng-model="model" btn-radio="1">click1</button><button ng-model="model" btn-radio="2">click2</button>', $scope);
Expand Down

0 comments on commit 02872dc

Please sign in to comment.