简体   繁体   English

从AngularJS版本1.0.8转换为1.4.2的问题

[英]Issues converting from AngularJS Version 1.0.8 to 1.4.2

I have a lovely date dropdown that works in AngularJS V1.0.8 and I am trying to run it using V1.4.2 but it doesn't seem to want to play ball. 我有一个可爱的日期下拉列表,适用于AngularJS V1.0.8,我试图使用V1.4.2运行它,但它似乎不想玩球。

What seems to be the issue? 这似乎是什么问题? As I have researched changes in the versions but can't see the problem. 由于我已经研究过版本的变化但是看不到问题。

I have got a plunker where you can see it working using 1.0.8 and then when you change the version on lines 5 and 8 to 1.4.2 it doesn't work. 我有一个plunker,你可以看到它使用1.0.8工作,然后当你将第5和第8行的版本更改为1.4.2时,它不起作用。

The fields should become invalid for dates like 31/02/2000 etc which is fine but not in 1.4.2 这些字段对于31/02/2000等日期应该无效,这是好的但不是1.4.2

What can the matter be? 问题是什么?

https://plnkr.co/edit/5ckBkzN6xYEvJvyoO0Ax?p=preview https://plnkr.co/edit/5ckBkzN6xYEvJvyoO0Ax?p=preview

angular.module('dateApp', []);
angular.module('dateApp'). 
 directive('dateTypeMulti', function () {
return {
  require: 'ngModel',
  link: function (scope, element, attrs, ngModel) {
    ngModel.$render = function () {
      angular.extend(scope.$eval(attrs.dateTypeMulti), ngModel.$viewValue);
    };

    scope.$watch(attrs.dateTypeMulti, function (viewValue) {
      ngModel.$setViewValue(viewValue); 
    }, true);

    ngModel.$formatters.push(function (modelValue) {
      if (!modelValue) return;

     var parts = String(modelValue).split('/');

      return {
        year: parts[0],
        month: parts[1],
        day: parts[2]
      };
    });

    ngModel.$parsers.unshift(function (viewValue) {
      var isValid = true,
          modelValue = '',
          date;

      if (viewValue) {
        date = new Date(viewValue.year, viewValue.month - 1, viewValue.day);
        modelValue = [viewValue.year, viewValue.month, viewValue.day].join('/');

        if ('//' === modelValue) {
          modelValue = '';
        } else if (
            date.getFullYear() != viewValue.year ||
            date.getMonth() != viewValue.month - 1 ||
            date.getDate() != viewValue.day) {
          isValid = false;
        }
      }

      ngModel.$setValidity('dateTypeMulti', isValid);

      return isValid ? modelValue : undefined;
    });
  }
};
})

Thanks 谢谢

Your directive stops working in 1.3.0-beta.10 , probably because of the following change: 您的指令在1.3.0-beta.10中停止工作,可能是因为以下更改:

ngModel: do not dirty the input on $commitViewValue if nothing was changed ngModel:如果没有更改,请不要弄脏$ commitViewValue上的输入

Since you are using an object as the view value: 由于您使用对象作为视图值:

scope.$watch(attrs.dateTypeMulti, function (viewValue) {
  ngModel.$setViewValue(viewValue); 
}, true);

The same object reference will be used, $commitViewValue will deem nothing to have changed and abort before the parse and validate pipeline gets started. 将使用相同的对象引用, $commitViewValue将在解析和验证管道启动之前认为没有任何更改和中止。

The documentation for $setViewValue states: $setViewValue的文档说明:

When used with standard inputs, the view value will always be a string (which is in some cases parsed into another type, such as a Date object for input[date].) However, custom controls might also pass objects to this method. 与标准输入一起使用时,视图值将始终为字符串(在某些情况下,它会被解析为另一种类型,例如输入[date]的Date对象。)但是,自定义控件也可能将对象传递给此方法。 In this case, we should make a copy of the object before passing it to $setViewValue. 在这种情况下,我们应该在将对象传递给$ setViewValue之前复制该对象。 This is because ngModel does not perform a deep watch of objects, it only looks for a change of identity. 这是因为ngModel不会对对象进行深度监视,它只会查找身份的更改。 If you only change the property of the object then ngModel will not realize that the object has changed and will not invoke the $parsers and $validators pipelines. 如果只更改对象的属性,则ngModel将不会意识到对象已更改,并且不会调用$ parsers和$ validators管道。 For this reason, you should not change properties of the copy once it has been passed to $setViewValue. 因此,一旦将副本传递给$ setViewValue,就不应该更改副本的属性。 Otherwise you may cause the model value on the scope to change incorrectly. 否则,您可能会导致范围上的模型值更改不正确。

Change to use angular.copy and it should work: 更改为使用angular.copy ,它应该工作:

ngModel.$setViewValue(angular.copy(viewValue))

Demo: https://plnkr.co/edit/kSS56n6LlHej25vcjfQq?p=preview 演示: https //plnkr.co/edit/kSS56n6LlHej25vcjfQq?p = preview

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

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