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

Commit 02872dc

Browse files
committed
feat(buttons): add controllerAs support
- Expose controller to the view for `btnCheckbox` and `btnRadio` directives Closes #4140
1 parent 88a885c commit 02872dc

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/buttons/buttons.js

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ angular.module('ui.bootstrap.buttons', [])
1414
return {
1515
require: ['btnRadio', 'ngModel'],
1616
controller: 'ButtonsController',
17+
controllerAs: 'buttons',
1718
link: function (scope, element, attrs, ctrls) {
1819
var buttonsCtrl = ctrls[0], ngModelCtrl = ctrls[1];
1920

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

src/buttons/test/buttons.spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ describe('buttons', function () {
1616
return el;
1717
};
1818

19+
it('should expose the controller to the view', inject(function ($templateCache) {
20+
var btn = compileButton('<button ng-model="model" btn-checkbox>{{button.text}}</button>', $scope);
21+
var ctrl = btn.controller('btnCheckbox');
22+
expect(ctrl).toBeDefined();
23+
24+
ctrl.text = 'foo';
25+
$scope.$digest();
26+
27+
expect(btn.html()).toBe('foo');
28+
}));
29+
1930
//model -> UI
2031
it('should work correctly with default model values', function () {
2132
$scope.model = false;
@@ -135,6 +146,17 @@ describe('buttons', function () {
135146
return el.find('button');
136147
};
137148

149+
it('should expose the controller to the view', inject(function ($templateCache) {
150+
var btn = compileButtons('<button ng-model="model" btn-radio="1">{{buttons.text}}</button>', $scope);
151+
var ctrl = btn.controller('btnRadio');
152+
expect(ctrl).toBeDefined();
153+
154+
ctrl.text = 'foo';
155+
$scope.$digest();
156+
157+
expect(btn.html()).toBe('foo');
158+
}));
159+
138160
//model -> UI
139161
it('should work correctly set active class based on model', function () {
140162
var btns = compileButtons('<button ng-model="model" btn-radio="1">click1</button><button ng-model="model" btn-radio="2">click2</button>', $scope);

0 commit comments

Comments
 (0)