Angularjs $parsers中的异步函数不';t更新$modelValue

Angularjs $parsers中的异步函数不';t更新$modelValue,angularjs,Angularjs,我想知道我应该如何在$parsers中处理异步函数。 下面的代码不更新作用域 我使用的是AngularJS 1.2,因此无法使用新的和奇特的1.3特性 标记: <input type="text" name="test" ng-model="test" parse> app.directive('parse', function($timeout) { return { require: 'ngModel', link: function(scope, ele

我想知道我应该如何在
$parsers
中处理异步函数。 下面的代码不更新作用域

我使用的是AngularJS 1.2,因此无法使用新的和奇特的1.3特性

标记:

<input type="text" name="test" ng-model="test" parse>
app.directive('parse', function($timeout) {
  return {
    require: 'ngModel',
    link: function(scope, element, attrs, ctrl) {
      ctrl.$parsers.unshift(function(viewValue) {
        $timeout(function() {
          return viewValue;
        });
      });
    }
  };
});

如果您正在寻找异步验证函数,我在不久前做了类似的事情,并将其作为库发布。检查自定义远程验证程序

基本思想是在从服务器收到验证结果后使用ngModelController$setValidity。这是指令源代码

.directive('customRemoteValidator', [function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, elm, attr, ngModelCtrl) {
            var validateFunctionNames = attr["remoteValidateFunctions"].split(",");
            var validatorNames = attr["customRemoteValidator"].split(",");
            ngModelCtrl.$parsers.push(function (value) {
                angular.forEach(validateFunctionNames, function (functionName, index) {
                    if (!scope[functionName]) {
                        console.log('There is no function with ' + functionName + ' available on the scope. Please make sure the function exists on current scope or its parent.');
                    } else {
                        var result = scope[functionName](value);
                        if (result.then) {
                            result.then(function (data) { //For promise type result object
                                ngModelCtrl.$setValidity(validatorNames[index], data);
                            }, function (error) {
                                ngModelCtrl.$setValidity(validatorNames[index], false);
                            });
                        }
                    }
                });
                return value;
            });
        }
    };
}])

你能详细描述一下你的情况吗。您可以调用$setViewValue(value);用于更新模型数据的模型控制器,但输入内容不会更改。您不能这样做。您能提供更多上下文吗?这是一个指令,用于对输入值的可用性进行异步查找。我基本上需要angular v1.3中的$asyncValidators,但我仍然坚持使用v1.2:(我有一个“变通方法”,但想知道如何正确处理这个问题:)变通方法只是返回viewValue,不管发生什么。所以
函数(viewValue){validatorAsync(viewValue);返回viewValue;}