如何删除整个html元素

时间:2017-10-21 23:10:01

标签: angularjs

我有以下列表:

enter image description here

Html代码:

var toyProblem = function () {
  var sol_str='';
  var operators = ['+','-','*','/'];
    for(var i in operators){
    	var prev_operator=(i-1);
    	if(sol_str!=''){sol_str+=operators[prev_operator];}
        sol_str +=arguments[i];
    }
    console.log(sol_str);
    return eval(sol_str);

 }


 console.log(toyProblem(6, 0, 10, 3));

但是只删除了文本,请参见下图: enter image description here

控制器代码:

 <div ng-repeat="alerta in alertas">
          <div class="aa" style="    margin-top: 1px;width: 100%; height: 52px;border-bottom: solid 1px #d4d4d4;">
                <div class="float-left" style="width: 80%;">
                    <a href="#" class="item item-icon-left"  >
                    <i class="icon ion-alert"></i>
                    {{alerta.desc}}
                    </a>
                </div>
                <div class="float-left divdeletea">
                     <span ng-click="deleteAlert($index)"><img class="imgDelete" src="img/remove-icon.png"></span>
                </div>
            </div>
       </div>

我预期的结果是删除整行: enter image description here

3 个答案:

答案 0 :(得分:1)

delete关键字用于从对象中删除属性。所以假设你有一个对象:

let object = {name: 'Pete'};
delete object.name; // {}

你留下一个空物体。我从未想过将它与数组一起使用,但是我假设它用undefined或类似的东西替换元素,这仍然是一个有效的数组元素。

无论如何,从数组中删除元素的正确方法是使用splice()方法,它采用两个参数,索引和要删除的元素数量。所以:

$scope.alertas.splice(index, 1);

答案 1 :(得分:0)

尝试splice从数组中删除项目:

$scope.deleteAlert= function(i){
    $scope.alertas.splice(i, 1);
}

答案 2 :(得分:0)

以下是一个可以帮助您的示例:

&#13;
&#13;
var app = angular.module("myApp", []); 
app.controller("myCtrl", function($scope) {
    $scope.alertas = [{desc:"test1"},{desc:"test2"},{desc:"test3"}];
    $scope.addItem = function (val) {
       $scope.alertas.push({desc:$scope.addMe});
    }    
    $scope.removeItem = function (x) {
        $scope.alertas.splice(x, 1);
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
{{alertas}}
  <ul>
    <li ng-repeat="alerta in alertas">{{alerta.desc}} <span ng-click="removeItem($index)">×</span></li>
  </ul>
  <input ng-model="addMe">
  <button ng-click="addItem()">Add</button>
</div>
&#13;
&#13;
&#13;