Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 输入指令中的setViewValue未更新实际可见输入值_Angularjs_Angularjs Directive_Angular Ngmodel - Fatal编程技术网

Angularjs 输入指令中的setViewValue未更新实际可见输入值

Angularjs 输入指令中的setViewValue未更新实际可见输入值,angularjs,angularjs-directive,angular-ngmodel,Angularjs,Angularjs Directive,Angular Ngmodel,我已经为此奋斗了将近两天。我希望你们能帮助我 总结: 我无法通过编程设置某些输入字段的视图值。 我有一个表单,其输入值在移除表单之前保存(可能有多个元素和多个表单,用户可能会关闭表单,然后重新打开)。重新打开表单时,我希望恢复以前的视图值(主要原因是恢复未保存在模型中的无效视图值)。这不管用 如果我调用ctrl.$setViewValue(previousValue),我会得到模型(可见)更新(如果有效),formControl的视图值(在控制台中调试时)也会更改,但我不会在输入字段中实际呈现它

我已经为此奋斗了将近两天。我希望你们能帮助我

总结:
我无法通过编程设置某些输入字段的视图值。
我有一个表单,其输入值在移除表单之前保存(可能有多个元素和多个表单,用户可能会关闭表单,然后重新打开)。重新打开表单时,我希望恢复以前的视图值(主要原因是恢复未保存在模型中的无效视图值)。这不管用

如果我调用ctrl.$setViewValue(previousValue),我会得到模型(可见)更新(如果有效),formControl的视图值(在控制台中调试时)也会更改,但我不会在输入字段中实际呈现它们。我不明白为什么:(

我把问题简化成这样:

javascript

var app = angular.module('App', [])

    function Controller($scope) {
        $scope.form = {
            userContent: 'initial content'
        }
    }
app.controller('Controller', Controller);
app.directive('resetOnBlur', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModel) {
            element.bind('blur', function () {
                console.log(ngModel);
                scope.$apply(setAnotherValue);
            });
            function setAnotherValue() {
                ngModel.$setViewValue("I'm a new value of the model. I've been set using the setViewValue method");
            }
        }
    };
});
Html

<form name="myForm" ng-app="App" ng-controller="Controller" class="form">
    Text: {{form.userContent}}
    <hr />
    If you remove the text, "Required!" will be displayed.<br/>
    If you change the input value, the text will update.<br/>
    If you blur, the text will update, but the (visible) input value not.
    <hr />
    <input class="input" type="text" ng-model="form.userContent" name="userContent" reset-on-blur required></textarea>
    <span ng-show="myForm.userContent.$error.required">Required!</span>
</form>

文本:{form.userContent}

如果删除文本,将显示“必需!”。
如果更改输入值,文本将更新。
如果模糊,文本将更新,但(可见)输入值不会更新。
必修的!
我希望你们能向我解释为什么这不起作用,以及如何解决这一问题…

您需要调用以在输入中反映viewvalue更改。在
$viewvalue上没有创建手表,因此更改会自动反映出来

   function setAnotherValue() {
        ngModel.$setViewValue("I'm a new value of the model. I've been set using the setViewValue method");
        ngModel.$render();
    }

$render
的默认实现执行以下操作:-

 element.val(ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue);

但是,您也可以覆盖和自定义
$render
的实现。

尝试scope.$apply()调用模型上的更改,因为您喜欢在ngModel初始化的作用域之外更改模型。调用$render就可以了。这很酷。但我确实需要覆盖$render,因为在1.3.0-rc.3$render()中看起来几乎相同,但它检查$isEmpty(ctrl.$modelValue)而不是$isEmpty(ctrl.$viewValue)。在setViewValue中,仅当模型值有效时才设置模型值。但我希望无效字段也能重新提交。在不覆盖渲染的情况下,使其行为与以前一样(如回答中所示)我的字段是空的。现在我已经重写了渲染方法。我想知道他们为什么更改了渲染方法。
scope.$apply()
对我不起作用。调用$setViewValue(…)我想已经触发了$apply所做的一切,因为当我调用$setViewValue时,该模型上的手表就熄灭了。