0
說我有以下幾點:將數據推送到一個對象數組角JS
.controller("chatCtrl", function($scope, $timeout, $rootScope) {
$scope.chats = [{
id: 1,
username: "Aname",
avatar: "imgsrc",
messages: [
"Hello",
"World"
]
}];
我如何去從表單數據推入消息數組?我有一個表格設置爲這樣:
%form{"ng-submit" => "add()"}
%input{:type => "text", :placeholder => "Enter a message", "ng-model" => "text"}
%input{:type => "submit", :value => "Send"}
和我的角像這樣:
$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消息數組。
@drakkin嘿,夥計們!是的,你會這麼想嗎?我以前試過,但我得到這個錯誤 - '錯誤:undefined不是一個對象(評估'$ scope.chats.messages.push') – Jackthomson
'$ scope.chats [0] .messages.push'工作嗎? – SlashmanX
@Drakkin嘿Drakking是的這是 – Jackthomson