怪异的Angular控制器行为
Weird Angular controller behavior
我有一个时间框架控件,用于增加/减少某个输入,其值以月为单位。右箭头增加输入的值(即1月-> 2月),左箭头减少输入的值,用户可以编辑输入本身以输入不同的月份。
但是,时间框架控件的行为确实很奇怪。当我点击其中一个错误时,它会将输入的值更改为""
。当我再次点击时,它会转到正确的月份。当我再次点击时,它会清除输入,在清除输入和进入正确的月份之间来回切换。
例如:连续点击5次右箭头,则输入的值为:
January
""
March
""
May
这个bug可能是什么?以下是我的相关代码:
myApp.controller('TimeframeCtrl', ['$scope',
'$cookieStore',
function ($scope, $cookieStore) {
// Create array of month names
var month_dictionary = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'];
// If month cookie doesn't exist
if ($cookieStore.get('month') == null) {
// Create month cookie
$cookieStore.put('month', 0);
}
// If year cookie doesn't exist
if ($cookieStore.get('year') == null) {
// Create year cookie
$cookieStore.put('year', 2014);
}
// Go to the previous month
$scope.prevMonth = function () {
// Handle corner cases of month looping
var month_helper = (
$cookieStore.get('month') - 1 == -1) ? 11 : $cookieStore.get('month') - 1;
// Update month scope variable
$scope.month = month_dictionary[month_helper];
// Update month cookie
$cookieStore.put('month', month_dictionary[month_helper]);
}
// Go to the next month
$scope.nextMonth = function () {
// Handle corner cases of month looping
var month_helper = ($cookieStore.get('month') + 1 == 12) ? 0 : $cookieStore.get('month') + 1;
// Update month scope variable
$scope.month = month_dictionary[month_helper];
// Update month cookie
$cookieStore.put('month', month_helper);
}
// Watch for manual editing of month
$scope.$watch('month', function () {
// Update month cookie
$cookieStore.put('month', month_dictionary.indexOf($scope.month));
// Remove warning class
$('.month-control').removeClass('warning-input');
});
// Set timeframe control values
$scope.month = month_dictionary[$cookieStore.get('month')];
// Test whether or not form inputs are valid
$scope.submitForm = function ($valid) {
// If so, update graphs
if ($valid) {
// Update graphs
// If not, add proper warning classes
} else {
if ($scope.month == undefined) {
$('.month-control').addClass('warning-input');
}
}
}
}]);
如果有人需要的HTML,我可以添加进去。非常感谢。
编辑:HTML
<form class="navbar-form pull-left" name="timeframeForm" ng-controller="TimeframeCtrl" ng-submit="submitForm(timeframeForm.$valid)">
<div class="timeframe"><i class="fa fa-calendar fa-l"></i>Timeframe</div>
<i class="fa fa-angle-left" ng-click="prevMonth()"></i>
<input class="form-control month-control" type="text" value="{{month}}" placeholder="Month" ng-model="month" ng-pattern="/January|February|March|April|May|June|July|August|September|November|December/g"/>
<i class="fa fa-angle-right" ng-click="nextMonth()"></i>
<button class="btn btn-refresh"><i class="fa fa-refresh"></i></button>
</form>
解决方案:
问题在于HTML中ng-pattern
正则表达式末尾的g
字符。从两个正则表达式中删除g
解决了这个问题。不确定确切的原因(这个资源解释得更好:正则表达式/_/g是什么意思?),但是看起来g
用空格替换了月份。不完全确定为什么在上每隔一次单击箭头键就会这样做,但很高兴问题解决了
相关文章:
- 如何在Angular单元测试中从另一个控制器的rootScope将方法添加到rootScope中
- Angular.js延迟控制器初始化
- 在控制器angular js之间共享数据
- 在一个控制器中更新服务变量,并在另一个控制器 - Angular JS中使用更新的值
- 无权访问我的控制器 Angular 中的属性.js 1.3
- 自动更新控制器Angular Metroet中的记录
- 显示控制器angular js的模态
- 解析未在控制器 [Angular JS] 中注入的函数
- 从输入中获取一个值,以便在控制器 Angular JS 中使用
- 如何调用另一个控制器(Angular js)中的函数
- 调用视图中的另一个控制器Angular
- 从另一个控制器Angular Js调用函数
- 在控制器- angular, Ionic中获取滚动后的ng-repeat索引
- 未知的供应商factoryprovider <-工厂<-控制器angular js
- 从控制器Angular JS中重构对象特定的逻辑
- 扩展UI选择控制器(angular)
- $scope变量值未在控制器Angular JS中更新
- 在控制器Angular中用ng点击定义html
- 如何将我的服务注入我的控制器angular js
- 将数据从服务器/应用程序.js传递到控制器 Angular-fullstack + multer