頁面上,我有以下 - 在這個PlunkerAngularJS對錶單輸入輸入指令,沒有儲蓄模型值
完整的代碼有一個自定義onEnter
指令壓在聊天的形式輸入時輸入調用一個函數。代碼段下面
//**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
指令它的作品。我在這裏錯過了什麼?任何幫助將是偉大的
啊沒有看到你也得到了它 – shaunhusain
個人我不喜歡這個解決方案,因爲它使用$ eval – Iamisti
在開始時我和@lamisti有同樣的觀點。然而,我意識到這個解決方案有很大的優勢 - 它不會創建隔離範圍,因此與隔離範圍內的其他潛在指令不會發生衝突,這可能會應用於同一個DOM元素。 – Klon