我在页面上有以下内容-此Plunker中的完整代码
onEnter
当在聊天表单输入上按下 enter 时,有一个自定义指令会调用一个函数。下面的代码片段
//**HTML View**
<div ng-controller="mainCtrl">
<ul>
<li ng-repeat="chat in chatMessages">
{{chat.username}}<br/>
{{chat.message}}
</li>
</ul>
</div>
<form id="chatForm" name="chatForm" ng-controller="formCtrl">
<label for="chat-username">User: </label>
<input type="text" id="chat-username" class="chat__username" ng-model="chat.username" required>
<label for="chat-input">Chat: </label>
<input type="text" id="chat-input" class="chat__input" on-enter="sendChat()" ng-model="chat.message" required>
<a href="#" class="chat__submit icon-comments" id="chat-submit" ng-click="sendChat()" ng-disabled="isChatValid()">Chatme</a>
</form>
//**Javascript**
app.controller('formCtrl',function($scope,Chats){
$scope.sendChat = function() {
if($scope.isChatValid()) {
return;
}
console.log(JSON.stringify($scope.chat));
var msg = {};
angular.copy($scope.chat,msg);
Chats.data.push(msg);
};
$scope.isChatValid = function() {
return $scope.chatForm.$invalid;
};
});
问题是输入 ( message
) 的值没有保存到范围模型 ( chat
) 中。如果我删除该onenter
指令,它会起作用。我在这里想念什么?任何帮助都会很棒