-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
187 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
app.controller('Ctrl8', function($scope) { | ||
$scope.roles = [ | ||
'guest', | ||
'user', | ||
'customer', | ||
'admin' | ||
]; | ||
$scope.user = { | ||
roles: ['user'] | ||
}; | ||
$scope.checkAll = function() { | ||
$scope.user.roles = angular.copy($scope.roles); | ||
}; | ||
$scope.uncheckAll = function() { | ||
$scope.user.roles = []; | ||
}; | ||
$scope.checkFirst = function() { | ||
$scope.user.roles.splice(0, $scope.user.roles.length); | ||
$scope.user.roles.push('guest'); | ||
}; | ||
$scope.getRoles = function() { | ||
return $scope.user.roles; | ||
}; | ||
$scope.check = function(value, checked) { | ||
var idx = $scope.user.roles.indexOf(value); | ||
if (idx >= 0 && !checked) { | ||
$scope.user.roles.splice(idx, 1); | ||
} | ||
if (idx < 0 && checked) { | ||
$scope.user.roles.push(value); | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
describe('function-as-model', function() { | ||
|
||
beforeEach(function() { | ||
browser().navigateTo(mainUrl); | ||
}); | ||
|
||
var s = '[ng-controller="Ctrl1"] '; | ||
var a = s+' input[type="checkbox"]'; | ||
|
||
it('should initialize with correct values', function() { | ||
check(a, [0,1,0,0]); | ||
expect(element(s+'pre').text()).toBe('[\n \"user\"\n]'); | ||
}); | ||
|
||
it('should check/uncheck items', function() { | ||
using(s+'label:eq(0)').input('checked').check(true); | ||
using(s+'label:eq(1)').input('checked').check(false); | ||
check(a, [1,0,0,0]); | ||
expect(element(s+'pre').text()).toBe('[\n \"guest\"\n]'); | ||
}); | ||
|
||
it('should check all', function() { | ||
element(s+'button[ng-click="checkAll()"]').click(); | ||
check(a, [1,1,1,1]); | ||
expect(element(s+'pre').text()).toBe('[\n \"guest\",\n \"user\",\n \"customer\",\n \"admin\"\n]'); | ||
}); | ||
|
||
it('should uncheck all', function() { | ||
element(s+'button[ng-click="uncheckAll()"]').click(); | ||
check(a, [0,0,0,0]); | ||
expect(element(s+'pre').text()).toBe('[]'); | ||
}); | ||
|
||
it('should check first', function() { | ||
element(s+'button[ng-click="checkFirst()"]').click(); | ||
check(a, [1,0,0,0]); | ||
expect(element(s+'pre').text()).toBe('[\n \"guest\"\n]'); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<label ng-repeat="role in roles"> | ||
<input type="checkbox" checklist-model="getRoles()" checklist-value="role" ng-change="check(role, checked)"> {{role}} | ||
</label> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52e4be0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying so hard to use this code, but I cannot get it to work. I need to display the selected values, which I can, but if a box is unchecked, I cannot get it to remove it. You have a value called "checked" that you pass to a function, but for the life of me, I can figure out where or how it gets set in the html.
52e4be0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you use a function as a model, then this is different. The model is only like a getter, there is no setter. This you have to do it manually, using something like
ng-change
, like in the example.52e4be0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
52e4be0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you create a plunkr or jsfiddle example?
52e4be0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
52e4be0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
52e4be0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you're not using the checklist-model as a function in that example. Btw, are you fine with moving our conversation to the issue list? Just submit a new issue with the plunkr example.
52e4be0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.