6

I have a website, where I have multiple buttons. Once a button is pressed I populate a list, though my problem is that the last pressed button keeps to be looking pressed (has the :active class). I thought about using angular's $timeout to reset the button, though the removeClass function doesn't do the trick.

My view looks like this:

    div(ng-controller='productButtonController')
    div(ng-repeat='product in products')
        div.col-md-4
            button.btn.btn-block.sell-button(id='{{product._id}}' ng-click='sell()'){{product.name}}

and my controller:

app.controller('productButtonController', ['$scope', '$timeout', 'productServices', 'flash',
    function($scope, $timeout, productServices, flash) {
        productServices.getProducts()
            .success(function(result) {
                $scope.products = result.data
            })
            .error(showErrorMessage(flash))

        $scope.sell = function() {
            console.log(this.product)
            that = this
            $('#' + that.product._id).removeClass('active')
        }
    }
])
Pio
  • 4,044
  • 11
  • 46
  • 81
  • What does your code look like for the button? I was running into an issue where I was using bootstraps toggle to change the appearance and angulars style conditionals at the same time which was causing the button to be double toggled, so it didn't display correctly. – Matthew Green Aug 08 '14 at 14:20
  • So show us your js/html code. If possible reproduce this fiddle. – Rahil Wazir Aug 08 '14 at 14:22

3 Answers3

8
  1. Add the angular $window service to your dependencies for the controller
  2. Call the blur method on the document's active element, which will be your button.

$window.document.activeElement.blur();

See How do you clear the focus in javascript?.

Community
  • 1
  • 1
benmod
  • 371
  • 6
  • 10
6

This code from Justin Poehnelt's handy GIST solves this elegantly.

app.directive('blur', [function () {
    return {
        restrict: 'A',
        link: function (scope, element) {
            element.on('click', function () {
                element.blur();
            });
        }
    };
}]);

Add the blur attribute to a button/element you need blurred after click. Eg.

<button type="button" blur>Click me</button>
dean grande
  • 734
  • 13
  • 15
5

If you simply want to override the focus status of the bootstrap buttons you could do it with:

.btn:focus{
    outline: none;
}

Then your buttons should look like:

<button class="btn btn-default">My button 1</button>

It's also important that the stylesheet which overrides the button status is loaded after the bootstrap stylesheet.

EDIT:

Sorry, but the previous step only removes the outline. The background-color of the button still remains the same.

Since bootstrap doesn't append any active classes to the clicked element as far as i know you need to change the :focus status of the button:

$('#' + that.product._id).blur();

Let me know if this works for you.

miron
  • 1,361
  • 1
  • 11
  • 24