选择值后,select,empty选项上的Ng值消失

时间:2018-03-03 21:00:12

标签: angularjs

我有一个ng-repeat遍历一个数组并加载我的选择选项,其中一个选项是一个空字符串,但如果我点击这些值,空选项会消失,我不知道为什么

这是我的选择

<select 
                        class="form-control input-sm" 
                        id="Select7" 
                        name="grossweightmeasurementunitcode" 
                        ng-model="itemForm.grossweightmeasurementunitcode" 
                        ng-readonly="itemForm.produtosempesobrutodefinido"
                        ng-required="!itemForm.produtosempesobrutodefinido">
                            <option ng-repeat="unidadepeso in unidadespeso" value="{{unidadepeso.commoncode}}" ng-selected="itemForm.grossweightmeasurementunitcode == unidadepeso.commoncode">{{unidadepeso.sigla}}</option>
                    </select>

我做了一个$ scope.unidadespeso.push ='';实现空值,任何想法?

1 个答案:

答案 0 :(得分:0)

将空项添加到数组。

以下示例可能会对您有所帮助。 如果您有任何疑问,请发表评论。

<!DOCTYPE html>
<html>

<head>
  <script data-require="jquery@3.0.0" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
  <link data-require="bootstrap@3.3.7" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  <script data-require="bootstrap@3.3.7" data-semver="3.3.7" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script data-require="angular.js@1.6.6" data-semver="1.6.6" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
  <script>
    (function() {

      angular.module("myapp", []).controller('MainCtrl', ['$scope', function($scope) {
          $scope.selectedItem = { name: 'two', id: 27 };
          $scope.items = [{name: 'one', id: 30 },{ name: 'two', id: 27 },{ name: 'threex', id: 50 }];
          
          var emptyItem = {name: '', id: '' };
          $scope.items.unshift(emptyItem);

      }]);
    }());
  </script>
  <style></style>
</head>

<body ng-app="myapp" ng-controller="MainCtrl">
  <p>selected item is : {{selectedItem}}</p>

  <p> name of selected item is : {{selectedItem.name}} </p>

  <select ng-model="selectedItem" ng-options="item.name for item in items track by item.id"></select>

</body>

</html>