Angular js:从ng-repeat中删除某些元素

时间:2013-12-18 10:50:46

标签: javascript angularjs angularjs-ng-repeat

我用ng-repeat构建按钮:

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

$scope.alpha = 'abcdefghijklmnopqrstuvwxyz';

问题是如何仅删除单击的按钮。我在按钮中使用了ng-hide,但随后所有按钮都消失了。 最好的方法是什么? 感谢

1 个答案:

答案 0 :(得分:3)

HTML:

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

JS:(相似)的

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/