var myApp = angular.module('myApp',[]);
myApp.directive('buttonGroup', function(){
return {
restrict: 'E',
scope: {
states: '=',
state: '=',
onStateChange: '='
},
template: '<div class="btn-group">' +
'<button class="btn" ng-repeat="s in states" ng-click="select(s, $event)">' +
'{{s}}' +
'</button>' +
'</div>',
replace: true,
link: function postLink(scope, elm, attrs, ctrl) {
console.log($(elm).find('.btn'));
// Make sure that style is applied to initial state value
scope.$watch(function () {
return $(elm).find('.btn').length; // it checks if the buttons are added to the DOM
}, function (newVal) {
// it applies the selected style to the currently defined state, if any
if (newVal > 0) {
$(elm).find('.btn').each(function(index, _elm){
if ($(_elm).text() == scope.state) $(_elm).addClass('btn-primary');
});
}
}, true);
// Apply style changes according to selection
scope.select = function(s, evt){
scope.state = s;
$(elm).find('.btn').removeClass('btn-primary'); // reset styles on all buttons
angular.element(evt.srcElement).addClass('btn-primary'); // apply style only to selected button
};
// Execute callback if it was provided
scope.$watch('state', function(){
if (scope.onStateChange){
scope.onStateChange();
}
}, true)
}
};
});
myApp.directive('highlightChanges', function(){
return {
scope: {
value: '='
},
link: function(scope, elm, attrs, ctrl) {
scope.$watch('value', function(){
var bgColor = $(elm).css('backgroundColor');
$(elm).text(scope.value);
$(elm).css('backgroundColor', 'orange');
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.