angularjs我正在使用多个选择标记如何在db中保存后清除其所有选项

时间:2014-12-02 12:04:32

标签: javascript angularjs

如何在保存到DB后清除多个选择选项,我使用ng-model清除它。它在后端清除但不在UI端。 在控制器中我写道:

smsType = {};
smsType.smsTypeId = [];

HTML code:

<div class="form-group">
 <select ng-model="smsType.smsTypeId" ui-jq="chosen" multiple
    class="w-md"
    ng-options="s.id as s.name for s in smsoption.name">
 </select>
</div>

它没有反映在ui方面

请给我一些建议我是angularjs的新手

2 个答案:

答案 0 :(得分:0)

模型是输出,而不是输入。要清除它,你需要清除smsoption。模型应该反映您选择的选项,而不是反过来。

smsoption = [];

应该这样做

答案 1 :(得分:0)

请参阅下面的演示

你错过了这里的范围

smsType.smsTypeId = [];

应该是

$scope.smsType.smsTypeId = [];

&#13;
&#13;
// Code goes here
angular.module("myApp", []);

angular.module("myApp").controller("SelectCtrl", ["$scope",
  function($scope) {
    $scope.smsoption = {
      name: [{
        "id": "122",
        "name": "denmark"
      }, {
        "id": "123",
        "name": "france"
      }, {
        "id": "124",
        "name": "italy"
      }]
    };

    $scope.saveit = function() {
      console.log($scope.selectCenter);
      $scope.smsType.smsTypeId = [];
    }
  }
]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<h1>ngOption demo</h1>
<div ng-app="myApp">
  <div ng-controller="SelectCtrl">

    <select ng-model="smsType.smsTypeId" ui-jq="chosen" multiple class="w-md" ng-options="s.id as s.name for s in smsoption.name">
    </select>
    <hr/>You have chosen:
    <span ng-repeat="type in smsType.smsTypeId">{{type}} | </span>
    <button ng-click="saveit()">Save</button>
  </div>
</div>
&#13;
&#13;
&#13;