search

Home  >  Q&A  >  body text

angular.js - angular ng-selected不起作用

1.html代码

 <select id="spgNumber"  ng-model="crystal.spacegroup" >
      <option ng-repeat="spaceGroup in spaceGroups"  ng-selected="$index==0"  value="{{$index+1}}">{{spaceGroup}}</option>
 </select>

2.js代码

$scope.crystal={
            lattice: [4, 4, 4, 90, 90, 90],
            spacegroup: 1,
            atoms: [{
                pos: [0, 0, 0],
                element: 1
            }],
            symmetry: [
                ["x", "y", "z"]
            ],
            centering: "PRIMITIVE",
        };
 $scope.spaceGroups=
       [ "P 1",              
        "P -1",                
        "P 1 2 1",             
        "P 1 2_1 1",           
        "C 1 2 1"]

3.问题描述:

  1)代码大概是option显示的text来自spaceGroups数组,其value是其在spaceGroups数组中的下标。select的值和crystal.spacegroup属性绑定,已实现。
  2)出现的问题是尽管我设置了ng-selected,select仍然没有默认选中值,我查看html的element是有selected="selected"属性的。
  如图:
 ![图片描述][1]
黄舟黄舟2744 days ago520

reply all(1)I'll reply

  • 世界只因有你

    世界只因有你2017-05-15 17:03:39

    I tried it using your code and it has a default value, portal

    reply
    0
  • Cancelreply