如何自定义此代码以使其在每次更改选择时执行一些指令。它采用默认值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;
}
});
提前致谢!
答案 0 :(得分:0)
首先,您的模型缺少对控制器的引用,您应该更改:
<select name="annee" ng-model="ffCtrl.annee">
然后您可以在ngModel上观看$或者只使用ng-change指令:
<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>
或在您的控制器内:
$scope.$watch('annee', function() { // My Code here });
答案 1 :(得分:0)
HTML:
<select name="year"
ng-model="annee"
ng-change="isSelected(annee)"
ng-options="year as year for year in yearList">
</select>
控制器:
function MyCtrl($scope) {
$scope.yearList=['2014','2015'];
$scope.annee=$scope.yearList[0];
$scope.isSelected=function(annee){
alert(annee);
}
}