Say I had the following:
说我有以下内容:
.controller("chatCtrl", function($scope, $timeout, $rootScope) {
$scope.chats = [{
id: 1,
username: "Aname",
avatar: "imgsrc",
messages: [
"Hello",
"World"
]
}];
How would I go about pushing data from a form into the messages array? I have a form setup as so:
我如何将表单中的数据推送到messages数组中?我有一个表单设置如下:
%form{"ng-submit" => "add()"}
%input{:type => "text", :placeholder => "Enter a message", "ng-model" => "text"}
%input{:type => "submit", :value => "Send"}
and my Angular as so:
和我的Angular一样:
$scope.text = '';
$scope.messages = [];
$scope.add = function() {
if($scope.text) {
$scope.messages.push(this.text);
$scope.text = '';
console.log($scope.messages);
}
}
Now of course this works because it is pushing it to the $scope.messages array I just defined, but I need to submit it to the $scope.chats messages array.
现在当然这是有效的,因为它将它推送到我刚刚定义的$ scope.messages数组,但我需要将它提交到$ scope.chats messages数组。
1 个解决方案
#1
0
you can use
您可以使用
$scope.text= '';
$scope.add = function() {
$scope.chats[0].messages.push($scope.text);
};
#1
0
you can use
您可以使用
$scope.text= '';
$scope.add = function() {
$scope.chats[0].messages.push($scope.text);
};