当我与ng模型绑定时,所选选项从视图中消失



我是离子/角度的新手。当我在(ng-model="mycomment.rating")中绑定ng模型时,默认选择的选项将从下拉视图中消失。

如果我不绑定,它就会再次出现。我的代码片段如下。有线索吗?

           <label class="item item-input item-select">
                <div class="input-label" >
                    Rating
                </div>
                  <select ng-model="mycomment.rating">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option selected>5</option>
                </select>

由于您将select元素绑定到模型,因此所选选项将是与模型值(作用域上的变量)相对应的选项。如果您希望默认的选定值为"5",请通过在控制器中初始化mycomment.rating来确保它等于5。在这种情况下,您甚至可以删除"selected"属性。

var m = angular.module('myApp', []);
m.controller('mainController', function($scope) {
  $scope.mycomment = {
    rating: 5
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="mainController">
  <span>{{mycomment.rating}}</span>
  <select ng-model="mycomment.rating">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
  </select>
</div>

最新更新