简体   繁体   English

获取$ watch不是AngularJS中的函数错误

[英]Getting $watch is not a function error in AngularJS

i'm trying to automatically update the END-DATE of my ui bootstrap datepicker when the START-DATE is greater than the END-DATE. 我尝试在START-DATE大于END-DATE时自动更新ui引导日期选择器的END-DATE。 The datepickers are inside a modal, and the function is done in AngularJS. 日期选择器位于模式内部,该功能在AngularJS中完成。

All the examples i've seen use $watch with $scope (ie $scope.$watch), but the code I have doesn't have $scope, not sure what the syntax would be in my case. 我见过的所有示例都将$ watch与$ scope一起使用(即$ scope。$ watch),但是我所拥有的代码没有$ scope,不确定我的语法是什么。

I'm getting: Error: vm.event.$watch is not a function 我得到: Error: vm.event.$watch is not a function

Html (min-date is set to "vm.event.startsAt" on the END-DATE HTML(最小日期在END-DATE设置为“ vm.event.startsAt”

      <div class="row">

                <div class="col-md-6">
                START DATE:<br />
                <p class="input-group" style="max-width: 250px">
                <input type="text" class="form-control" readonly datepicker-popup="medium" ng-model="vm.event.startsAt" is-open="vm.event.startOpen" close-text="Close" />
                <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="vm.toggle($event, 'startOpen', vm.event)"><i class="glyphicon glyphicon-calendar"></i></button>
                </span>
                </p>
                <timepicker ng-model="vm.event.startsAt" hour-step="1" minute-step="15" show-meridian="true"></timepicker>
                </div>

                <div class="col-md-6">
                END DATE:<br />
                <p class="input-group" style="max-width: 250px">
                <input type="text" class="form-control" readonly datepicker-popup="medium" ng-model="vm.event.endsAt" min-date="vm.event.startsAt" is-open="vm.event.endOpen" close-text="Close" />
                <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="vm.toggle($event, 'endOpen', vm.event)"><i class="glyphicon glyphicon-calendar"></i></button>
                </span>
                </p>
                <timepicker ng-model="vm.event.endsAt" hour-step="1" minute-step="15" show-meridian="true"></timepicker>
                </div>
      </div>     

AngularJS AngularJS

'use strict';

angular
  .module('demo', ['mwl.calendar', 'ui.bootstrap', 'ngTouch', 'ngAnimate', 'ui.bootstrap.demo'])

  .controller('MainCtrl', function ($modal, moment, $http, $location) {

/* 
******************************************
****  Show Event Modal
******************************************
*/
    function showEventModal(action, event, events) {

      var modalInstance;

        modalInstance = $modal.open({
        templateUrl: 'modalEventContent.html',
        controller: function() {
          var vm = this;
          vm.action = action;
          vm.event = event;

          vm.events = events;

          vm.toggle = function($event, field, event) {
                $event.preventDefault();
                $event.stopPropagation();
                vm.event[field] = !vm.event[field];
          };

          // ----------------------------             
          // ------  Problem HERE -------
          // ----------------------------                         
          // ------  Update END DATE based on the START DATE 
          vm.event.$watch('startsAt', function (newVal, oldVal) {
            if(!newVal) return;

            // if the new start date is greater than the current end date .. update the end date
            if(vm.event.endsAt){
              if( +vm.event.endsAt < +vm.event.startsAt ){
                vm.event.endsAt = newVal;
              }
            } else {
              // just set the end date
              vm.event.endsAt = newVal;
            }
          });   


          vm.eventSaved = function(event) { 

               $http.put(urlapievents + event.eventid, vm.event).success(function(eventsuccess){
               }).error(function(err){
                    /* do something with errors */
               });

               modalInstance.close();
          };

          modalInstance.close();
          };

        },
        controllerAs: 'vm'
      });
    }

update#1 更新#1

Added the $scope dependency to my controller, the error is gone but the function doesn't react to the change 向我的控制器添加了$ scope依赖项,该错误消失了,但是该功能对更改没有反应

      // ------  Update END DATE based on the START DATE (only valid for calendar view)
      $scope.$watch('vm.event.startsAt', function (newVal, oldVal) {
        if(!newVal) return;

        // if the new start date is greater than the current end date .. update the end date
        if(vm.event.endsAt){
          if( +vm.event.endsAt < +vm.event.startsAt ){
            vm.event.endsAt = newVal;
          }
        } else {
          // just set the end date
          vm.event.endsAt = newVal;
        }
      });   

add the $scope dependency to your controller: $scope依赖项添加到您的控制器中:

.controller('MainCtrl', function ($scope, $modal, moment, $http, $location) { ..}

that's why the $watch couldn't be found, because it belongs to a scope object which did not exist. 这就是为什么找不到$watch的原因,因为它属于一个不存在的作用域对象。

So the reason why you are having an issue here is because $watch lives only on the $scope object. 因此,这里出现问题的原因是$ watch仅位于$ scope对象上。 Because you are using the bindToController syntax you do not use scope and therefore it doesn't exist. 因为您使用的是bindToController语法,所以不使用范围,因此它不存在。

If you wanted to use a $watch in your controller (which is not recommended) , you can do so by injecting $scope with Dependency injection. 如果要在控制器中使用$watch (不建议使用) ,则可以通过将$scope与Dependency Injection注入来实现。 Then you can attach the $watch with $scope.$watch 然后您可以将$watch$scope.$watch

If you want to use a $watch , it is better to use in a directive. 如果要使用$watch ,最好在指令中使用。 It can be hard to test overall. 可能很难进行整体测试。

There are certain timepickers like ui-bootstraps uib-timepicker which support the ng-change directive for event handling. 有某些时间选择器,例如ui-bootstraps uib-timepicker ,它们支持ng-change指令进行事件处理。

So if your <timepicker></timepicker> element can use ng-change you can add that on your element and then call the same validation function you are using in your controller. 因此,如果您的<timepicker></timepicker>元素可以使用ng-change ,则可以将其添加到元素中,然后调用您在控制器中使用的相同验证函数。

Here is a plunker using the ng-change of a timepicker 这是一个使用时间选择器的ng-change的pl子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM