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

Commit 3e30cd9

Browse files
committedJul 16, 2013
feat(buttons): support dynamic true / false values in btn-checkbox
Closes #666
1 parent 63ae7e1 commit 3e30cd9

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed
 

‎src/buttons/buttons.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,25 @@ angular.module('ui.bootstrap.buttons', [])
4141
require:'ngModel',
4242
link:function (scope, element, attrs, ngModelCtrl) {
4343

44-
var trueValue = scope.$eval(attrs.btnCheckboxTrue);
45-
var falseValue = scope.$eval(attrs.btnCheckboxFalse);
44+
function getTrueValue() {
45+
var trueValue = scope.$eval(attrs.btnCheckboxTrue);
46+
return angular.isDefined(trueValue) ? trueValue : true;
47+
}
4648

47-
trueValue = angular.isDefined(trueValue) ? trueValue : true;
48-
falseValue = angular.isDefined(falseValue) ? falseValue : false;
49+
function getFalseValue() {
50+
var falseValue = scope.$eval(attrs.btnCheckboxFalse);
51+
return angular.isDefined(falseValue) ? falseValue : false;
52+
}
4953

5054
//model -> UI
5155
ngModelCtrl.$render = function () {
52-
element.toggleClass(activeClass, angular.equals(ngModelCtrl.$modelValue, trueValue));
56+
element.toggleClass(activeClass, angular.equals(ngModelCtrl.$modelValue, getTrueValue()));
5357
};
5458

5559
//ui->model
5660
element.bind(toggleEvent, function () {
5761
scope.$apply(function () {
58-
ngModelCtrl.$setViewValue(element.hasClass(activeClass) ? falseValue : trueValue);
62+
ngModelCtrl.$setViewValue(element.hasClass(activeClass) ? getFalseValue() : getTrueValue());
5963
ngModelCtrl.$render();
6064
});
6165
});

‎src/buttons/test/buttons.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ describe('buttons', function () {
6363
expect($scope.model).toEqual(0);
6464
expect(btn).not.toHaveClass('active');
6565
});
66+
67+
it('should monitor true / false value changes - issue 666', function () {
68+
69+
$scope.model = 1;
70+
$scope.trueVal = 1;
71+
var btn = compileButton('<button ng-model="model" btn-checkbox btn-checkbox-true="trueVal">click</button>', $scope);
72+
73+
expect(btn).toHaveClass('active');
74+
expect($scope.model).toEqual(1);
75+
76+
$scope.model = 2;
77+
$scope.trueVal = 2;
78+
$scope.$digest();
79+
80+
expect(btn).toHaveClass('active');
81+
expect($scope.model).toEqual(2);
82+
});
6683
});
6784

6885
describe('radio', function () {

0 commit comments

Comments
 (0)