将输入的值放入AngularJS中的另一个输入表单中

时间:2018-03-28 20:13:59

标签: javascript angularjs

有这个小提琴

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

app.controller('MyController', ['$scope', MyController]);    

function MyController($scope) {

    $scope.value1 = $scope.value1;
    $scope.value2 = "this is it: " + $scope.value1;
}
<html>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">

</script>
<div ng-controller='MyController'>
    <h1>Introduce your value:</h1>
    <input type="text" ng-model="value1"></input></br>
    <input type="text" ng-model="name" ng-change="value1"></input>
</div>
</html>

我想在第一个输入中引入一个值,并将其作为“this it it:firstvalue”进入下一个输入。

1 个答案:

答案 0 :(得分:1)

您可以使用ng-change并正确使用ng-model

轻松实现这一目标

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

app.controller('MyController', ['$scope', MyController]);    

function MyController($scope) {

    $scope.value1 = undefined;
    $scope.value2 = undefined;
    
    $scope.onValue1Change = function(){
      $scope.value2 = "this is it: " + $scope.value1;
    };
}
<html ng-app="myApp">

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
</script>
<div ng-controller='MyController'>
    <h1>Introduce your value:</h1>
    <input type="text" ng-model="value1" ng-change="onValue1Change()"></input></br>
    <input type="text" ng-model="value2"></input>
</div>
</html>