Javascript 如何正确控制收音机并使用角度选择值

Javascript 如何正确控制收音机并使用角度选择值,javascript,angularjs,Javascript,Angularjs,我试图设置radio的值并选择页面加载的元素,如果当前URL包含type和annee参数,那么我将这些参数设置为radio的值,如果不包含,则选择else,我将它们分别设置为1和2014 我使用此代码来处理此问题: html 但这行不通。仅当URL中未设置参数且仅将typeradio设置为1时,它才起作用 请问我该怎么修 非常感谢 我认为您的问题是type_param,因此this.type是string类型,但是您的numRadio参数是int类型。因此它检查f.e.2==“2”,这是fals

我试图设置radio的值并选择页面加载的元素,如果当前URL包含
type
annee
参数,那么我将这些参数设置为radio的值,如果不包含,则选择else,我将它们分别设置为1和2014

我使用此代码来处理此问题:

html

但这行不通。仅当URL中未设置参数且仅将
type
radio设置为1时,它才起作用

请问我该怎么修


非常感谢

我认为您的问题是
type_param
,因此
this.type
是string类型,但是您的numRadio参数是int类型。因此它检查f.e.2==“2”,这是false


因此您可以执行
this.type=(type\u param!='undefined'?parseInt(type\u param):1)

使用工作代码和稍微不同的方法检查我的plunker,我已删除所有ng更改以使其更干净

Tout
4 MOI
6 MOI
8 MOI
12兆欧

为什么不使用带有复选框和收音机的ng模型?@maurycy,是的,请查看html发布的代码。抱歉,我不知怎么错过了它
<span class="form-group" ng-controller="FiltreFicheController as ffCtl">
    <input type="radio" name="type" ng-model="type" value="1" style="margin-left:10px;" ng-change="ffCtl.checkRadio(1)" ng-checked="ffCtl.isChecked(1)"> Tout
    <input type="radio" name="type" ng-model="type" value="4" ng-change="ffCtl.checkRadio(4)" ng-checked="ffCtl.isChecked(4)"> 4 mois
    <input type="radio" name="type" ng-model="type" value="6" ng-change="ffCtl.checkRadio(6)" ng-checked="ffCtl.isChecked(6)"> 6 mois
    <input type="radio" name="type" ng-model="type" value="8" ng-change="ffCtl.checkRadio(8)" ng-checked="ffCtl.isChecked(8)"> 8 mois
    <input type="radio" name="type" ng-model="type" value="12" ng-change="ffCtl.checkRadio(12)" ng-checked="ffCtl.isChecked(12)"> 12 mois
    <select name="annee" ng-model="annee">
        <option value="2014" ng-selected="ffCtrl.isSelected(2014)">2014</option>
        <option value="2000" ng-selected="ffCtrl.isSelected(2000)">2000</option>
    </select>
</span>
app.controller('FiltreFicheController', function($scope, $location){
        var link = document.URL;
        var type_param = (!location.search.indexOf('?', link)?location.search.split('type=')[1].split('&')[0]:'undefined');
        var annee_param = (!location.search.indexOf('?', link)?location.search.split('annee=')[1]:'undefined');
        this.type = (type_param!=='undefined'?type_param:1);
        this.annee = (annee_param!=='undefined'?annee_param:2014);

        this.isChecked = function(numRadio){
            return this.type === numRadio;
        }

        this.isSelected = function(annee){
            alert(link);
            return this.select === annee;
        }

        this.checkRadio = function(numRadio){
            this.type = numRadio;
            window.location.href = link.substring(link.indexOf('/'),link.lastIndexOf('/')) + '/fiche?type=' + numRadio + '&annee=' + this.annee;
        }
    });
<input type="radio" name="type" ng-model="type" value="1" style="margin-left:10px;" > Tout
<input type="radio" name="type" ng-model="type" value="4" > 4 mois
<input type="radio" name="type" ng-model="type" value="6" > 6 mois
<input type="radio" name="type" ng-model="type" value="8" > 8 mois
<input type="radio" name="type" ng-model="type" value="12" > 12 mois