0

How to customize this code to make it execute some instructions on each change of selection. And which takes as default value this.annee.

html

<span class="form-group" ng-controller="FiltreFicheController as ffCtl">
    <select name="annee" ng-model="annee">
        <option value="2014" ng-selected="ffCtrl.isSelected(2014)" ng-change="ffCtrl.selectElt(2014)">2014</option>
        <option value="2000" ng-selected="ffCtrl.isSelected(2000)" ng-change="ffCtrl.selectElt(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'?parseInt(type_param):1);
        this.annee = (annee_param!=='undefined'?parseInt(annee_param):2014);

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

        this.selectElt = function(numElt){
            this.annee = numElt;
            window.location.href = link.substring(link.indexOf('/'),link.lastIndexOf('/')) + '/fiche?type=' + this.type + '&annee=' + numElt;
        }
    });

Thanks in advance!

Artyom Neustroev
  • 8,627
  • 5
  • 33
  • 57
user3821280
  • 39
  • 1
  • 7

2 Answers2

0

First of all, your Model is missing the reference to the controller, you should change that:

<select name="annee" ng-model="ffCtrl.annee">    

Then you can either $watch on the ngModel or you just use the ng-change directive:

 <select name="annee" ng-model="ffCtrl.annee" ng-change="ffCtrl.myFunction()">
    <option value="2014" ng-selected="ffCtrl.isSelected(2014)" ng-change="ffCtrl.selectElt(2014)">2014</option>
    <option value="2000" ng-selected="ffCtrl.isSelected(2000)" ng-change="ffCtrl.selectElt(2000)">2000</option>
</select> 

or inside your controller:

$scope.$watch('annee', function() { // My Code here });
David Losert
  • 4,652
  • 1
  • 25
  • 31
0

html:

   <select name="year"
        ng-model="annee"
        ng-change="isSelected(annee)" 
        ng-options="year as year for year in yearList">
     </select>

Controller:

function MyCtrl($scope) {
   $scope.yearList=['2014','2015'];
   $scope.annee=$scope.yearList[0];
    $scope.isSelected=function(annee){
        alert(annee);
    }
}

For your reference

Francis Stalin
  • 439
  • 1
  • 4
  • 14
  • But the problem is that the final date on which the count will stops is the current year. How to deal with that? – user3821280 Jul 18 '14 at 08:34
  • Note that when I copy/paste your code it doesn't work. Would you please seperate them and define each piece where will it be pasted ? – user3821280 Jul 18 '14 at 08:37