我需要预先填充一个<textarea>使用json项

I need to pre-fill a <textarea> with a json item

本文关键字:textarea json 使用 一个 填充      更新时间:2023-09-26

我发现我可以通过以下方式做到这一点,没有问题,直到我到达我还需要可编辑的地方,并在更新后保存。

下面是一个用于添加当前项目json数据的指令:(发现于SO) - AngularJS: textarea bind to json object show "object-object"

    app.directive('jsonText', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attr, ngModel) {
            function into(input) {
                return JSON.parse(input);
            }
            function out(data) {
                return JSON.stringify(data);
            }
            ngModel.$parsers.push(into);
            ngModel.$formatters.push(out);
        }
    };
});

这里是html,这适用于预填充,但我需要ngModel也拉进来,这样我就可以在编辑时保存数据。

<textarea json-text ng-model="review" id="review" class="form-control" rows="3"  ng-bind="encounterNote"></textarea>

所以最后我需要一种方法来预填充(可能是jSON API的几行文本),允许编辑,然后在保存函数上,我已经引用了"当前"值来保存和更新。

@devinallenaz是正确的,仅仅在textarea中使用ngModel就像有魅力一样。