最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS: How do I use the $destroy method to remove an element - Stack Overflow

programmeradmin0浏览0评论

Very new Angular and super-confused as to how to use the $destroy method to remove an element as mentioned in the API doc here:

.$rootScope.Scope

Here is the HTML I tried:

<div ng-controller="TodosController">
    <div class="tasks" ng-show="todos">
        <ul ng-repeat="todo in todos">
            <li>              
                    <button ng-click="todos.$destroy(todo)">Delete</button>
                    <b>{{todo.name}}</b>                
            </li>
        </ul>
    </div>
</div>

And the JS:

var myApp = angular.module('myApp', []);

function TodosController($scope) {
    $scope.todos = [{
        name: "Learn angular",
        estimate: 8,
        done: true},
    {
        name: "Install java",
        estimate: 2,
        done: false},
    {
        name: 'Uninstall ruby',
        estimate: 3,
        done: false}];
}

Here is my fiddle for above: /

In older version of Angular, it is possible to use the $remove method to remove an element.

<script type="text/javascript" ng:autobind src=".9.16/angular-0.9.16.js"></script>

<div ng:controller="TodosController">
    <div class="tasks" ng:show="todos">
        <ul ng:repeat="todo in todos">
            <li>              
                <div ng:controller="TodoEditorController">
                    <button ng:click="todos.remove(todo)">Delete</button>
                        <b>{{todo.name}}</b>
                </div>

            </li>
        </ul>
    </div>
</div>

The above is working in this Fiddle:

/

However, using $destroy(), $remove(), and remove() doesn't work in later version of Angular. Any suggestions?

Very new Angular and super-confused as to how to use the $destroy method to remove an element as mentioned in the API doc here:

http://docs.angularjs/api/ng.$rootScope.Scope

Here is the HTML I tried:

<div ng-controller="TodosController">
    <div class="tasks" ng-show="todos">
        <ul ng-repeat="todo in todos">
            <li>              
                    <button ng-click="todos.$destroy(todo)">Delete</button>
                    <b>{{todo.name}}</b>                
            </li>
        </ul>
    </div>
</div>

And the JS:

var myApp = angular.module('myApp', []);

function TodosController($scope) {
    $scope.todos = [{
        name: "Learn angular",
        estimate: 8,
        done: true},
    {
        name: "Install java",
        estimate: 2,
        done: false},
    {
        name: 'Uninstall ruby',
        estimate: 3,
        done: false}];
}

Here is my fiddle for above: http://jsfiddle/5Mzda/26/

In older version of Angular, it is possible to use the $remove method to remove an element.

<script type="text/javascript" ng:autobind src="http://code.angularjs/0.9.16/angular-0.9.16.js"></script>

<div ng:controller="TodosController">
    <div class="tasks" ng:show="todos">
        <ul ng:repeat="todo in todos">
            <li>              
                <div ng:controller="TodoEditorController">
                    <button ng:click="todos.remove(todo)">Delete</button>
                        <b>{{todo.name}}</b>
                </div>

            </li>
        </ul>
    </div>
</div>

The above is working in this Fiddle:

http://jsfiddle/bpTXe/195/

However, using $destroy(), $remove(), and remove() doesn't work in later version of Angular. Any suggestions?

Share Improve this question asked Mar 19, 2013 at 9:28 ChanporyChanpory 3,0956 gold badges39 silver badges49 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

$destroy is for destroying $scopes.

array.$remove has been removed in version 0.10.6 bubblewrap-cape (2012-01-17)

You can use array.splice:

<button ng-click="todos.splice(key,1)">Delete</button>
发布评论

评论列表(0)

  1. 暂无评论