Javascript 奇怪的角度控制器行为

Javascript 奇怪的角度控制器行为,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我有一个时间框架控件,它增加/减少某个输入的时间,该输入的值以月为单位。有一个右箭头增加输入值,即一月->二月,一个左箭头减少输入值,用户可以编辑输入本身以在不同的月份输入 然而,timeframe控件的行为真的很奇怪。当我单击其中一个错误时,它会将输入值更改为。当我再次单击时,它将转到正确的月份。当我再次单击时,它会清除输入,并在清除输入和转到正确月份之间来回移动 例如:如果我连续单击右箭头5次,输入值将为: January "" March "" May 这个bug可能是什么?这是我的相关

我有一个时间框架控件,它增加/减少某个输入的时间,该输入的值以月为单位。有一个右箭头增加输入值,即一月->二月,一个左箭头减少输入值,用户可以编辑输入本身以在不同的月份输入

然而,timeframe控件的行为真的很奇怪。当我单击其中一个错误时,它会将输入值更改为。当我再次单击时,它将转到正确的月份。当我再次单击时,它会清除输入,并在清除输入和转到正确月份之间来回移动

例如:如果我连续单击右箭头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

解决方案:


问题是HTML中ng模式正则表达式末尾的g字符。从两个正则表达式中删除g解决了这个问题。不确定为什么这个资源可以更好地解释它:,但似乎g用空格代替了月份。不完全清楚为什么每隔点击一次箭头键,但很高兴问题解决了

不知道它是否与您的问题有关,但行$cookieStore.put'month',month_dictionary[month_helper];应该是$cookieStore。输入'month',month\u helper;在上个月不是吗?是啊。。只是改变了。似乎并没有解决问题,但这是一个很好的发现。适用于我:mocked$scope和$cookieStore。所以要么问题出在你没有展示给我们的代码中,要么这里发生了其他事情。是的,我一直在跟踪$scope.month本身的价值,它工作得很好。介意我发布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>