假设我在列表中有5个文本框。我在第一个文本框中输入文本输入,然后单击“发送”。现在我希望输入的输入值显示在列表中的所有其他文本框中。
以下是我的HTML代码: -
<!DOCTYPE html>
<html>
<head>
<title>AngularJS Plunker</title>
<script src="./scripts/angular.min.js"></script>
<script src="./scripts/app.js"></script>
</head>
<body ng-app="chatApp">
<div ng-controller="chatController">
<ul>
<li ng-repeat="chat in chats">
<input type="text"/>
<button ng-click="sendChat()">Send</button>
<button ng-click="deleteChat($index)">Delete</button>
</li>
</ul>
<button ng-click="addChat()">Click me to add</button>
</div>
</body>
</html>
以下是我的角度代码: -
var app=angular.module('chatApp',[]);
app.controller('chatController',['$scope',function($scope){
$scope.chats=[];
$scope.addChat = function() {
if($scope.chats.length<10)
{
$scope.chats.push({name:''});
}
}
$scope.deleteChat=function(index){
$scope.chats.splice(index,1);
}
$scope.sendChat=function(data){
}
}]);
我有一个sendChat函数,我想放置代码。
答案 0 :(得分:2)
在输入字段中添加模型
<input type="text" ng-model="chat.msg"/>
然后传递选定的消息
<button ng-click="sendChat(chat)">Send</button>
然后在其他文本框中分配msg
$scope.sendChat = function(data) {
$scope.chats.map(function(x) {
x.msg = data.msg;
})
}
的 DEMO
强>