如何正确控制无线电并使用角度选择值

时间:2014-07-17 13:39:26

标签: javascript angularjs

我正在尝试设置广播的值并在页面加载上选择元素,如果当前网址包含typeannee参数,那么我将这些参数设置为广播的值如果没有,则选择其他,我将它们分别设置为1和2014。

我正在使用此代码来解决此问题:

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>

JS

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;
        }
    });

但这不起作用。仅当URL中没有设置参数且仅将type无线电设置为1时,它才有效。

我该如何解决这个问题?

非常感谢!

2 个答案:

答案 0 :(得分:1)

我认为您的问题是type_param因此this.type的类型为字符串,但您的numRadio参数的类型为int。所以检查f.e. 2 ===“2”这是假的。

所以你可以做this.type = (type_param!=='undefined'?parseInt(type_param):1);

答案 1 :(得分:0)

使用工作代码和不同的方法检查我的plunker,我已删除所有ng-change以使其更清洁

http://plnkr.co/edit/w61cKtp1eNnL31EL5GPR?p=preview

<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