简体   繁体   English

角1.4.7 ngModel.NgModelController。$ parsers不调用函数

[英]angular 1.4.7 ngModel.NgModelController.$parsers not call function

I have problem with ngModel.NgModelController.$parsers. 我对ngModel.NgModelController。$ parsers有问题。 I explain it at example: I create directive for input field. 我在示例中进行解释:我为输入字段创建指令。 Each time I change value in this input in html then it should call my registered function. 每次我在html中更改此输入中的值时,它应该调用我的注册函数。

And this registered function should always change what you write in input field to 'A' char, but modify model. 并且此注册功能应始终将您在输入字段中写的内容更改为“ A”字符,但要修改模型。

html file : html文件

<div ng-app="myApp">
  <form>
    <input ng-model="myval1" somedir/>
  </form>
</div>

and js file : js文件

var myApp = angular.module('myApp', []);
myApp.directive("somedir", function(){
  return {
    restrict:'A',
    require: '?ngModel',
    link: function(scope, element, attrs, ngModelController) {
      ngModelController.$parsers.unshift(function(val){
        console.log("i'm here");
        ngModelController.$viewValue ="A";
        ngModelController.$render();
        return val;
      });

    }
  }
});

I prepare jsfiddle to tests: 我准备jsfiddle进行测试:

angular 1.2.9 : http://jsfiddle.net/hwzxfon3/1/ 角1.2.9: http : //jsfiddle.net/hwzxfon3/1/

angular 1.4.7 : http://jsfiddle.net/372re41m/1/ 角1.4.7: http : //jsfiddle.net/372re41m/1/

And in angular 1.2.9 it work correct. 在角度1.2.9中,它可以正常工作。 In angular 1.4.7 there is some problem. 在角度1.4.7中存在一些问题。 How to get it: focus on input field, and press 3 times same button eg '1'. 如何获取:专注于输入字段,然后按3次相同的按钮,例如“ 1”。 First time it show 'A' in input field and in browser console i'm here . 第一次在输入字段和浏览器控制台中显示“ A”时, i'm here Second time also the same. 第二次也一样。 But third time it show 'A'+pushed char (eg 'A1') and not write in browser console i'm here which means that registered function was not called. 但是第三次​​显​​示“ A” +按下字符(例如“ A1”)并且没有在浏览器控制台中写入, i'm here ,这意味着未调用注册功能。

Please tell me if, I have bug in my code (please write what to change), or it is bug in angular (then i report bug to angular team)? 请告诉我,如果我的代码中有错误(请写更改内容),或者是角度错误(然后我向角度小组报告错误)? I test it in latest chrome and safari in os x (10.10.5). 我在os x(10.10.5)的最新chrome和safari中对其进行了测试。 If it works for you without problem please write this too. 如果它对您没有问题,请也写下此内容。

I report this problem to angular team. 我将这个问题报告给了角力团队。 Full response is there https://github.com/angular/angular.js/issues/13068 . 完整的响应在那里https://github.com/angular/angular.js/issues/13068

In short, in similar situation it is better to use this: 简而言之,在类似情况下,最好使用以下命令:

ngModelController.$setViewValue("A");

then this: 然后这个:

ngModelController.$viewValue ="A";

Working jsfiddle for angular 1.4.7: http://jsfiddle.net/455qdd6p/1/ 适用于angular 1.4.7的jsfiddle: http : //jsfiddle.net/455qdd6p/1/

I needed it to solve for my angular module https://github.com/uhlryk/angular-dynamic-number 我需要它来解决我的角度模块https://github.com/uhlryk/angular-dynamic-number

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

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