简体   繁体   中英

Ng-click and ng-repeat - angularJs doesn't work

I know about different scopes inside ng-repeat, I headed about this but I still can't solute removing of selected item of li ( I don't use $index ) instead of future sortable objects

<li class="m-1" ng-repeat="students in students.students">
  <span>{{students.student}}</span> :knowledge is - <span>{{students.knowledge}}</span>
  <button type="button" class="btn btn-danger ml-2" ng-click="students.removeItem(item)">Delete</button>
</li>

and my function working behind li-element, but delete only last element

vm.removeItem = function (item) {
  vm.students.splice(vm.students.indexOf(item), 1);
}

and I don't need $parent in ng-repeat

Maybe you are trying to do something like:

<li class="m-1" ng-repeat="student in students.students">
  <span>{{student.name}}</span> :knowledge is - <span>{{student.knowledge}} </span>
  <button type="button" class="btn btn-danger ml-2" ng- click="removeItem(student)">Delete</button>
</li>

$scope.removeItem = function (item) {
    this.students.students = this.students.students.filter(student => student !== item)
};

This is a working demo

Honestly, I don't understand why you have students object in students. In addition, why every item in ng-repeat is called: students?

I guess that every item needs the variable name of student .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM