如何获得选定的特定项目?

时间:2023-01-19 10:27:12
  • screenshot attached.
  • 截图。

I learning angularJS. And i can't find a way to remove the selected item that the 'Remove' button was click on.

我学习angularJS。而且我也找不到一种方法来移除“移除”按钮所选择的项目。

Is there any way to do it ?

有什么办法吗?

code attached:

代码连接:

        <ul class="unstyled">
            <li ng-repeat="todo in todos">
                <input type="checkbox" ng-model="todo.done">
                <span class="done-{{todo.done}}">{{todo.text}}</span>
                <button class="btn" ng-click="removeTodo()">Remove</button>
            </li>
        </ul>

如何获得选定的特定项目?

1 个解决方案

#1


3  

var app = angular.module("app" , []);
app.controller("MyCtrl" , function($scope){
  
  $scope.todos = [
    {"text" :"Learn AngularJS","done":false},{"text" :"build an app","done":false}];
  
  $scope.removeTodo = function(index) {
    $scope.todos.splice(index,1);
    
    }
  
  $scope.removeTodo2 = function(todo) {
    var index = getByValue( $scope.todos,todo);
    $scope.todos.splice(index,1);
    
    }
  
  $scope.addTodo = function(todo){
    var toDoObject = {"text":todo,"done":false};
    $scope.todos.push(toDoObject);
    
  };
  
  $scope.done = function(todo){
    angular.forEach($scope.todos,function(index,todo1){
        if(todo == todo1)
           $scope.todos[index].done = !$scope.todos[index].done;
      })
   
    
    }
  
  function getByValue(arr, value) {

  for (var i=0, iLen=arr.length; i<iLen; i++) {

    if (arr[i].text == value) return i;
  }
}
  
  });
.done{
  text-decoration: line-through;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
  
 <ul class="unstyled">
            <li ng-repeat="todo in todos track by $index">
                <input type="checkbox" ng-model="todo.done" >
                <span ng-class="{'done' : todo.done == true}">{{todo.text}}</span>
                <button class="btn" ng-click="removeTodo($index)">Remove</button>
               <button class="btn" ng-click="removeTodo2(todo.text)">Remove2</button>
            </li>
        </ul>
  <input type="text" ng-model="todo">
  <input type="button" ng-click = "addTodo(todo)" value="Add">
  
  </div>

#1


3  

var app = angular.module("app" , []);
app.controller("MyCtrl" , function($scope){
  
  $scope.todos = [
    {"text" :"Learn AngularJS","done":false},{"text" :"build an app","done":false}];
  
  $scope.removeTodo = function(index) {
    $scope.todos.splice(index,1);
    
    }
  
  $scope.removeTodo2 = function(todo) {
    var index = getByValue( $scope.todos,todo);
    $scope.todos.splice(index,1);
    
    }
  
  $scope.addTodo = function(todo){
    var toDoObject = {"text":todo,"done":false};
    $scope.todos.push(toDoObject);
    
  };
  
  $scope.done = function(todo){
    angular.forEach($scope.todos,function(index,todo1){
        if(todo == todo1)
           $scope.todos[index].done = !$scope.todos[index].done;
      })
   
    
    }
  
  function getByValue(arr, value) {

  for (var i=0, iLen=arr.length; i<iLen; i++) {

    if (arr[i].text == value) return i;
  }
}
  
  });
.done{
  text-decoration: line-through;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
  
 <ul class="unstyled">
            <li ng-repeat="todo in todos track by $index">
                <input type="checkbox" ng-model="todo.done" >
                <span ng-class="{'done' : todo.done == true}">{{todo.text}}</span>
                <button class="btn" ng-click="removeTodo($index)">Remove</button>
               <button class="btn" ng-click="removeTodo2(todo.text)">Remove2</button>
            </li>
        </ul>
  <input type="text" ng-model="todo">
  <input type="button" ng-click = "addTodo(todo)" value="Add">
  
  </div>