[英]Push Data Into An Objects Array With Angular JS
说我有以下几点:
.controller("chatCtrl", function($scope, $timeout, $rootScope) {
$scope.chats = [{
id: 1,
username: "Aname",
avatar: "imgsrc",
messages: [
"Hello",
"World"
]
}];
我将如何将数据从表单推入message数组? 我有这样一个表单设置:
%form{"ng-submit" => "add()"}
%input{:type => "text", :placeholder => "Enter a message", "ng-model" => "text"}
%input{:type => "submit", :value => "Send"}
和我的Angular一样:
$scope.text = '';
$scope.messages = [];
$scope.add = function() {
if($scope.text) {
$scope.messages.push(this.text);
$scope.text = '';
console.log($scope.messages);
}
}
现在当然可以了,因为它会将其推送到我刚刚定义的$ scope.messages数组中,但是我需要将其提交到$ scope.chats messages数组中。
您可以使用
$scope.text= '';
$scope.add = function() {
$scope.chats[0].messages.push($scope.text);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.