如何使用另一个textarea编辑textarea值?

时间:2019-10-21 18:42:54

标签: javascript jquery angularjs angularjs-directive

我有一个Textarea框,其值是“一些初始内容” 我想使用另一个Textarea框编辑其值

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.myParagraphContent = "Some{{SecondTextareaContent}} initial content.";
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

<p>{{myParagraphContent}}</p>

<textarea ng-model="SecondTextareaContent"></textarea>
<textarea ng-model="myParagraphContent"></textarea>

</div>

3 个答案:

答案 0 :(得分:0)

更新

您可以改用ngChange。查看演示here

对于Javascript部分:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
     $scope.SecondTextareaContent = '';
     $scope.myParagraphContent = 'Some initial content.';
     $scope.onChange = function() {
        $scope.myParagraphContent = 'Some ' + $scope.SecondTextareaContent + ' initial content.';
     };
});

对于HTML部分:

<textarea ng-model="SecondTextareaContent" ng-change="onChange()"></textarea>
<textarea ng-model="myParagraphContent"></textarea>

旧答案

对于Javascript部分:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
     $scope.SecondTextareaContent = '';
     $scope.myParagraphContent = 'Some initial content.';
     $scope.onKeyDown = function() {
        $scope.myParagraphContent = 'Some ' + $scope.SecondTextareaContent + ' initial content.';
     };
});

对于HTML部分:

<textarea ng-model="SecondTextareaContent" ng-keydown="onKeyDown()"></textarea>
<textarea ng-model="myParagraphContent"></textarea>

答案 1 :(得分:0)

您可以执行以下操作;

<div ng-app="myApp" ng-controller="myCtrl">

<p>{{myParagraphContent}}</p>

<textarea ng-model="SecondTextareaContent" ng-change='myParagraphContent=SecondTextareaContent'></textarea>
<textarea ng-model="myParagraphContent"  ></textarea>

</div>

如果您还想进行ng-change,也可以使用一些函数来执行。

答案 2 :(得分:-1)

我试图了解你。这样的东西?

const txt = document.querySelectorAll(".txt");
const setValue = () => {
  txt[1].value = txt[0].value;
}
txt[0].addEventListener("input", setValue, false); 
<textarea class="txt"></textarea>
<textarea class="txt"></textarea>