1

我是angularjs的新手,並試圖找到一個解決方案,將選定的電臺組值設置爲ngmodel。收音機組選擇的選項不更新角度的ngModel值

//my.html  

<div ng-controller='controller'> 
<div class="btn-group" ng-model="option" ng-repeat="arr in dragDropOption"> 
    <input type="radio" name="optionCorrectOpt" data-ng-model="option" 
    value="{{dragDropOption[$index].text}}"> 
     {{dragDropOption[$index].text}} 
</div> 

和 //mycontroller.js

app = angular.module('app', []); 

app.controller('controller', function ($scope) { 
$scope.dragDropOption = []; 
$scope.option = "not set"; 

$scope.dragDropOption = [{ 
    text: "analog" 
}, { 
    text: "isdn" 
}, { 
    text: "dsl" 
}]; 
// $scope.option = $scope.dragDropOption[0].text; 
}); 

My fiddle is here

可能是重複的問題,請幫我分享已經回答了的stackoverflow問題的鏈接或新的答案。提前致謝。

+0

你'arr'內循環,所以爲什麼用'dragDropOption [$指數]'? – Yoshi

+0

@Yoshi它是「arr.text」,而不是「dragDropOption [$ index] .text」這是在小提琴上調試代碼。 –

回答

2

對於input變化

data-ng-model="option" 

到:

data-ng-model="$parent.option" 

演示Fiddle

+0

其作品適合我。非常感謝 –

+1

你也可以返回'$ scope.option = $ scope.dragDropOption [0] .text;' –

2

更換

$scope.option = "not set"; 

<input type="radio" name="optionCorrectOpt" data-ng-model="option" 
value="{{dragDropOption[$index].text}}"> 

要:

$scope.radioOption = {}; 
$scope.radioOption.selected = "not set" 

<input type="radio" name="optionCorrectOpt" data-ng-model="radioOption.selected" 
value="{{dragDropOption[$index].text}}"> 

JS FIDDLE