【问题标题】:Removing the Item from the dropdown once its added using angular使用角度添加后从下拉列表中删除项目
【发布时间】:2015-05-29 04:14:52
【问题描述】:

HTML 代码

<!doctype html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8">
  <title>AngularJS Plunker</title>
  <link rel="stylesheet" href="style.css">
  <script>
    document.write("<base href=\"" + document.location + "\" />");
  </script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
  <script src="script.js"></script>
</head>

<body ng-controller="MainCtrl">
  <h1> NG options</h1>
  <form name="addUser">
    Application:
    <select ng-model="filterAddUser.application" ng-init ="filterAddUser.application = 'STACK'" title="" ng-options="value as value for  (key , value) in applicationStatus">
    </select>
    Roles:
    <select ng-model="filterAddUser.role" title="" ng-init ="filterAddUser.role = 'R'" ng-options="role.value as role.param for role in roleStatus">
    </select>

    <button ng-click="addToCart()">AddItem</button>

    <div class="addCart">
      <ul ng-repeat="item in items">
        <li><b>Application:</b> {{item.application}}</li>
        <li><b>Role:</b> {{item.role}}</li>
        <li class="actionOptions">
          <button ng-click="toggleSelected($index)">removeItem</button>
        </li>
      </ul>
    </div>
  </form>
</body>
</html>

Javascript 代码

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

app.controller('MainCtrl', function($scope) {

  $scope.items = [];

  $scope.applicationStatus = {
    "TEST App": "TEST",
    "ABC App": "ABC",
    "TRY App": "TRY",
    "SIR App": "SIR",
    "LOPR App": "LOPR",
    "STACK App": "STACK"
  };
  $scope.roleStatus = [{
    "param": "Read",
    "value": "R"
  }, {
    "param": "Write",
    "value": "W"
  }, {
    "param": "Admin",
    "value": "A"
  }, {
    "param": "Super Approver",
    "value": "SA"
  }, {
    "param": "Supervisor",
    "value": "S"
  }];


  $scope.addToCart = function() {

    $scope.items.push({
      application: $scope.filterAddUser.application,
      role: $scope.filterAddUser.role
    });
    // Clear input fields after push
    $scope.filterAddUser['application'] = "";
    $scope.filterAddUser['role'] = "";
  }

  $scope.toggleSelected = function(index) {
    $scope.items.splice(index, 1);
  };

});

我要做的就是当我将应用程序添加到购物车时,该应用程序需要从 dropdwon 中删除,并且当我单击需要推回购物车的删除项目时,我已经包含了一个plunker 以及http://plnkr.co/edit/kSsetX?p=preview 需要帮助。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    更新了你的 plunkr:http://plnkr.co/edit/QQobh7Jx76r7lDzw7TzV

           var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
      $scope.items = [];
      var deletedApplication = [];
    
      $scope.applicationStatus = {
        "TEST App": "TEST",
        "ABC App": "ABC",
        "TRY App": "TRY",
        "SIR App": "SIR",
        "LOPR App": "LOPR",
        "STACK App": "STACK"
      };
    
      $scope.roleStatus = [{
        "param": "Read",
        "value": "R"
      }, {
        "param": "Write",
        "value": "W"
      }, {
        "param": "Admin",
        "value": "A"
      }, {
        "param": "Super Approver",
        "value": "SA"
      }, {
        "param": "Supervisor",
        "value": "S"
      }];
    
      $scope.filterAddUser = {
        application: $scope.applicationStatus[0],
        role: $scope.roleStatus[0]
      };
    
    
      $scope.addToCart = function() {
        deletedApplication.push([
          $scope.filterAddUser.application,  $scope.applicationStatus[$scope.filterAddUser.application]
        ]);
    
        delete $scope.applicationStatus[$scope.filterAddUser.application];
    
        $scope.items.push({
          application: $scope.filterAddUser.application,
          role: $scope.filterAddUser.role
        });
    
        // Clear input fields after push
        $scope.filterAddUser['application'] = $scope.applicationStatus[0];
        $scope.filterAddUser['role'] = $scope.roleStatus[0];
    
      }
    
      $scope.toggleSelected = function(index) {
        var addApp = deletedApplication.filter(function(deletedApp){
          return deletedApp[0] === $scope.items[index].application;
        })[0];
    
        $scope.applicationStatus[addApp[0]] = addApp[1];  
        console.log($scope.applicationStatus);
        $scope.items.splice(index, 1);
      };
    
    });
    

    【讨论】:

    • 我会在一分钟内更新你的 plunkr 并更新我的答案。另外,我刚刚注意到您的 plunkr 使用的代码与您发布的代码不同,这就是我的困惑所在!
    • 这段代码应该让你知道应该去哪里。如果您需要更多帮助,请告诉我。
    • 再次更新。让我知道这是否适合你。这有点棘手,因为 applicationStatus 是一个对象。 Plunkr 以及答案中的代码已更新
    • 感谢凯文,这正是我想要的,我感谢你的帮助,我是新手,你能帮我提高技能吗?我被这个问题困扰了三天:(
    • 没问题!我已经使用 Angular 几年了,我也花了一段时间才弄清楚,所以不要太气馁。像数组一样迭代对象键从来都不是一件有趣的事
    猜你喜欢
    • 2017-12-28
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多