I have the following on a page - full code in this Plunker
There is a custom onEnter
directive that calls a function when enter is pressed on a chat form input. Code snippet below
//**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;
};
});
Problem is the value of the input (message
) is not saved into the scope model (chat
). If I remove the onenter
directive it works. What am I missing here? Any help will be great