0

I built buttons with ng-repeat:

<button ng-repeat="alphabet in alpha" ng-click="checkAlpha()" value="{{alphabet}}">{{alphabet}}</button>

$scope.alpha = 'abcdefghijklmnopqrstuvwxyz';

The question is how to remove only the button that is clicked. I used ng-hide in button, but then all the buttons are gone. What is the best way to do that? Thanks

3
  • ng-click="checkAlpha($index)" and provide implementation in checkAlpha() method to handle the deletion by index :) Commented Dec 18, 2013 at 10:55
  • 2
    You can use $index. There is a jsfiddle here jsfiddle.net/SX4gE/11 Commented Dec 18, 2013 at 10:56
  • Good fiddle, I've updated it to match the question :) Upvoted. Commented Dec 18, 2013 at 11:13

1 Answer 1

3

HTML:

 <div ng-controller='ctrl'>
    <button ng-repeat='alphabet in alpha ' ng-click="checkAlpha($index)" value="{{alphabet}}" id="{{$index}}">{{alphabet}}</button>
</div>

JS:(similar)

angular.module("app", []).controller("ctrl", function ($scope) {
    //lets create array from a string.
    $scope.alpha = 'abcdefghijklmnopqrstuvwxyz'.split("");

    $scope.checkAlpha = function(index) {
        $scope.alpha.splice(index, 1);//remove
    }
});

FIDDLE:

http://jsfiddle.net/SX4gE/20/

Sign up to request clarification or add additional context in comments.

3 Comments

The remove() is not necessary and no Angular way. Removing the character from the array is sufficient to remove the button (see here).
@Holybreath Thank youu :) is it possible, to all the other buttons to stay in their position(absolut) after one button is clicked?
Updated the fiddle ;) jsfiddle.net/SX4gE/20 , if it's fine by you that ng-repeat is used on the div containing the buttons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.