4

如果这个值是一个对象而不是一个字符串,我如何为收音机设置一个默认值?

编辑:

为了让事情更清楚,我更新了小提琴:查看小提琴:http: //jsfiddle.net/JohannesJo/CrH8a/

<body ng-app="app">
<div ng-controller='controller'>
            oConfigTerminal value= <input type="text" ng-model="oConfigTerminal"/><br><br>

    <div ng-show="!oConnection.aOptions"
    ng-repeat="oConnection in oFormOptions.aStationaryConnections">
        <label>
            <input type="radio" name="connection" ng-model="$parent.oConfigTerminal" value="{{oConnection.id}}"
            />{{oConnection.sId}}</label>
    </div>

</div>

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

app.controller('controller', function ($scope) {
$scope.oFormOptions = {};
$scope.oConfigTerminal=0;
$scope.oFormOptions.aStationaryConnections = [{
    id: 1,
    sId: "analog"
}, {
    id: 2,
    sId: "isdn"

}, {
    id: 3,
    sId: "dsl"
}];

// !!! Trying to set the default checked/selected value !!!
$scope.oConfigTerminal = $scope.oFormOptions.aStationaryConnections[0];
});
4

1 回答 1

2

在浏览器控制台中检查实时 html,您会看到这oConnection是一个object,并且 radio 的值变为:{"id":1,"sId":"analog"}。您可能想oConnection.id使用价值

另一个问题ng-repeat是范围继承问题,需要ng-model通过设置ng-model来解决$parent.variableName

oConnectionTmpView对我没有任何意义,因此为了简化起见,我将其删除:

HTML:

<div ng-show="!oConnection.aOptions" ng-repeat="oConnection in oFormOptions.aStationaryConnections">
  <label>
       <input type="radio"  
              name="connection" 
              ng-model="$parent.oConfigTerminal" 
               value="{{oConnection.id}}"
         />
           {{oConnection.sId}}
     </label>
 </div>

JS:

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

app.controller('controller', function ($scope) {
    $scope.oFormOptions = {};
    $scope.oConfigTerminal = 0;
    $scope.oFormOptions.aStationaryConnections = [{
        id: 1,
        sId: "analog"
    }, {
        id: 2,
        sId: "isdn"

    }, {
        id: 3,
        sId: "dsl"
    }];
}

演示:http: //jsfiddle.net/CrH8a/14/

于 2013-03-10T21:25:09.350 回答