在ng-repeat(angular-js)内设置收音机的默认值

时间:2021-10-11 13:27:44

How can I set a default value for the radios if this value is an object and not a string?

如果此值是对象而不是字符串,如何设置无线电的默认值?

EDIT:

编辑:

To make things more clear I updated 's fiddle: Check out the fiddle: http://jsfiddle.net/JohannesJo/CrH8a/

为了让事情更清楚,我更新了小提琴:查看小提琴: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];
});

1 个解决方案

#1


2  

Inspect the live html in browser console and you will see that oConnection is an object and value of radio becomes: {"id":1,"sId":"analog"}. You probably want to use oConnection.id for value

在浏览器控制台中检查实时html,你会看到oConnection是一个对象,无线电的值变为:{“id”:1,“sId”:“analog”}。您可能希望使用oConnection.id作为值

One other issue within ng-repeat there is a scope inheritance issue that needs to be resolved for ng-model by setting ng-model to $parent.variableName

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

Your oConnectionTmpView didn't make any sense to me so for simplification I removed it:

你的oConnectionTmpView对我没有任何意义,所以为了简化我删除了它:

HTML:

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:

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"
    }];
}

Demo: http://jsfiddle.net/CrH8a/14/

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

#1


2  

Inspect the live html in browser console and you will see that oConnection is an object and value of radio becomes: {"id":1,"sId":"analog"}. You probably want to use oConnection.id for value

在浏览器控制台中检查实时html,你会看到oConnection是一个对象,无线电的值变为:{“id”:1,“sId”:“analog”}。您可能希望使用oConnection.id作为值

One other issue within ng-repeat there is a scope inheritance issue that needs to be resolved for ng-model by setting ng-model to $parent.variableName

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

Your oConnectionTmpView didn't make any sense to me so for simplification I removed it:

你的oConnectionTmpView对我没有任何意义,所以为了简化我删除了它:

HTML:

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:

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"
    }];
}

Demo: http://jsfiddle.net/CrH8a/14/

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