angular.js - angular radio repeat 默认选中值
巴扎黑
巴扎黑 2017-05-15 16:59:56
0
2
613

html代码如下:

<ul>
  <li ng-repeat="thing in favoriteThings">
    <input type="radio" ng-value="thing" ng-model="selected" name="stuff"/>
      {{thing.text}}
  </li>
</ul>

js代码如下:

$scope.favoriteThings = [
    {nr: 1, text: "Raindrops on roses"},
    {nr: 2, text: "Whiskers on kittens"},
    {nr: 3, text: "Bright copper kettles"},
    {nr: 4, text: "Warm woolen mittens"},
    {nr: 5, text: "Brown paper packages tied up with strings"},
    {nr: 6, text: "Cream colored ponies"},
    {nr: 7, text: "Crisp apple streudels"},
    {nr: 8, text: "Doorbells"},
    {nr: 9, text: "Sleigh bells"},
    {nr: 10, text: "Schnitzel with noodles"},
    {nr: 11, text: "Wild geese that fly with the moon on their wings"},
    {nr: 12, text: "Girls in white dresses with blue satin sashes"},
    {nr: 13, text: "Snowflakes that stay on my nose and eyelashes"},
    {nr: 14, text: "Silver white winters that melt into springs"}
  ];
  
  $scope.selected = {nr: 1, text: "Raindrops on roses"};

目前我这样做,不能默认选中一个值,也不知道问题出在哪里。

我想页面一加载就默认选中一个值,其中每一项的值类型是对象,我应该如何做?

巴扎黑
巴扎黑

membalas semua(2)
漂亮男人

Perlu menetapkan nilai lalai model ng
seperti:

  $scope.selected = $scope.favoriteThings[0];

Pilihan ini akan dipilih secara lalai.

Tidak boleh digunakan

  $scope.selected = {nr: 1, text: "Raindrops on roses"};

Oleh kerana hanya rujukan yang sama kepada Object dalam model ng akan dianggap sama Kaedah di atas mencipta objek baharu, yang tidak sama dengan nilai dalam ulangan

某草草

boleh digunakan dalam ng-repeat ng-checked, contohnya:

<label ng-repeat="(key, val) in genders track by $index">
    <input type="radio" name="gender" value="{{key}}" ng-checked="list.gender==key">{{val}}
</label>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan