Html 在AngularJS中单击按钮更改选定值
我正在尝试更改按钮单击上的selectbox的值。 我有一个包含所有月份的选择框,当我单击“上一个月”按钮时,我想将所选值更改为上一个月的值,但现在的值无法正常工作。它只改变一次 这是我的HTML:Html 在AngularJS中单击按钮更改选定值,html,angularjs,drop-down-menu,Html,Angularjs,Drop Down Menu,我正在尝试更改按钮单击上的selectbox的值。 我有一个包含所有月份的选择框,当我单击“上一个月”按钮时,我想将所选值更改为上一个月的值,但现在的值无法正常工作。它只改变一次 这是我的HTML: <select name="months" ng-model="monthFromSelector" ng-change="getSelectVal('monthFromSelector')"> <option ng-repeat="month in months" valu
<select name="months" ng-model="monthFromSelector" ng-change="getSelectVal('monthFromSelector')">
<option ng-repeat="month in months" value="{{month}}">{{month}}</option>
</select>
<button ng-click="prevMonth()">Vorige maand</button>
发生这种情况是因为您总是从当前的中获取上一个月的信息:
thisMonth = $scope.months[now.getMonth() -1]
$scope.prevMonth = function() {
let selectedIndex = $scope.months.indexOf($scope.monthFromSelector);
if(selectedIndex > 0) {
$scope.monthFromSelector = $scope.months[selectedIndex - 1];
}
}
您需要从所选月份中获取上一个月的数据:
thisMonth = $scope.months[now.getMonth() -1]
$scope.prevMonth = function() {
let selectedIndex = $scope.months.indexOf($scope.monthFromSelector);
if(selectedIndex > 0) {
$scope.monthFromSelector = $scope.months[selectedIndex - 1];
}
}
检查工作演示:之所以发生这种情况,是因为您总是从当前的中获取上个月的数据:
thisMonth = $scope.months[now.getMonth() -1]
$scope.prevMonth = function() {
let selectedIndex = $scope.months.indexOf($scope.monthFromSelector);
if(selectedIndex > 0) {
$scope.monthFromSelector = $scope.months[selectedIndex - 1];
}
}
您需要从所选月份中获取上一个月的数据:
thisMonth = $scope.months[now.getMonth() -1]
$scope.prevMonth = function() {
let selectedIndex = $scope.months.indexOf($scope.monthFromSelector);
if(selectedIndex > 0) {
$scope.monthFromSelector = $scope.months[selectedIndex - 1];
}
}
检查正在运行的演示:我想你已经复制粘贴了这一行
thisMonth = $scope.months[now.getMonth() -1]
这条线每次都是当前月份
将此行替换为以下行将正常工作
thisMonth = $scope.months[$scope.months.indexOf($scope.monthFromSelector) - 1]
检查工作示例:
angular.module('myapp',[])
.controller('myctrlr',['$scope',函数($scope){
$scope.message='message';
$scope.months=[“一月”、“二月”、“马尔特”、“四月”、“梅”、“朱尼”、“朱利”、“奥古斯都”、“九月”、“奥克托伯”、“十一月”、“十二月”];
var now=新日期();
var thismount=$scope.months[now.getMonth()];
$scope.monthFromSelector=本月;
$scope.prevMonth=函数(){
thisMonth=$scope.months[$scope.months.indexOf($scope.monthFromSelector)-1]
$scope.monthFromSelector=本月;
console.log(本月)
}
}]);代码>
{{month}
沃里格·马兰德
我想你已经复制粘贴了这一行
thisMonth = $scope.months[now.getMonth() -1]
这条线每次都是当前月份
将此行替换为以下行将正常工作
thisMonth = $scope.months[$scope.months.indexOf($scope.monthFromSelector) - 1]
检查工作示例:
angular.module('myapp',[])
.controller('myctrlr',['$scope',函数($scope){
$scope.message='message';
$scope.months=[“一月”、“二月”、“马尔特”、“四月”、“梅”、“朱尼”、“朱利”、“奥古斯都”、“九月”、“奥克托伯”、“十一月”、“十二月”];
var now=新日期();
var thismount=$scope.months[now.getMonth()];
$scope.monthFromSelector=本月;
$scope.prevMonth=函数(){
thisMonth=$scope.months[$scope.months.indexOf($scope.monthFromSelector)-1]
$scope.monthFromSelector=本月;
console.log(本月)
}
}]);代码>
{{month}
沃里格·马兰德