在更新$ scope变量时查看未更新

时间:2014-12-12 07:41:28

标签: javascript angularjs view angular-ngmodel

在这里,我尝试了一个代码,只是简单地将日历月值增加1,代码工作正常,由控制台中的值反映,但视图没有更新。我无法理解我出错的地方。请告知我,因为我对角度行为不太熟悉。意图是在右箭头单击时增加月份,例如2015年1月,并在左箭头单击时减小值。代码工作正常,但视图永远不会更新。


查看:

<div class="ctr-align btm-bdr"><a href="javascript:;"><img src="images/redarrow_bac.png" align="absmiddle" width="24" height="32" alt="" ng-click="decrMonth()"></a> &nbsp; {{month}} {{year}} &nbsp; <a href="javascript:;" ><img src="images/redarrow.png" align="absmiddle" width="24" height="32" alt="" ng-click="incrMonth()"></a></div>
如果 $ scope.month $ scope.year 在js中更新。

UI
enter image description here

控制器

//---------------------------- Loading next month on the arrows pressed ----------------------------------------
    // Constructor
    $scope.initJobsController           = function(){
        if(!UserServices.isLoggedIn()){
            HeaderService.setHeader(header_config.HEADER_LOGIN);
            $location.path('/login');
        }
        else{
            HeaderService.hideHeader();
            $scope.dateToday = new Date();
            $scope.setCalendarMonthHead($scope.dateToday);
            $scope.jobsLoadingTitle      = msg_config.JOBS_LOADING;
            $scope.jobsLoading           = true;
            JobsServices.getMyJobList($scope.startIndex);
            var timer= $timeout(function(){
                $scope.replaceFont();
            },0.5)
        }
    };
    // Description : Function that increments the month by 1
    $scope.incrMonth                    = function(){
        $scope.dateToday.setMonth($scope.dateToday.getMonth() + 1);
        $scope.setCalendarMonthHead($scope.dateToday);
        $scope.$apply();
    };
    // Description : Function that decrement the month by 1
    $scope.decrMonth                    = function(){
        $scope.dateToday.setMonth($scope.dateToday.getMonth() - 1);
        $scope.setCalendarMonthHead($scope.dateToday);
        $scope.$apply();
    };
    // Description : Function that decrement the month by 1
    $scope.setCalendarMonthHead         = function(date){
        var monthNames   = [ "January", "February", "March", "April", "May", "June",
                                "July", "August", "September", "October", "November", "December" ];
        $scope.month      = monthNames[$scope.dateToday.getMonth()];
        $scope.year       = $scope.dateToday.getFullYear();
        // alert($scope.month + " " + $scope.year);
    };

即使 $ scope。$ apply(); 似乎也无法正常工作,尝试了此处提到的所有可能性AngularJS view not updating on model changehttp://jimhoskins.com/2012/12/17/angularjs-and-apply.html,{{3因为它们都围绕着使用 $ scope。$ apply()

0 个答案:

没有答案