我想點擊一個按鈕後,添加一些文字上一個光標位置。AngularJS的NG-模型ICM textarea的
在控制器:
$scope.addEmoji = function(name){
var element = $("#chat-msg-input-field");
element.focus(); //ie
var selection = element.getSelection();
var textBefore = $scope.chatMsg.substr(0, selection.start);
var textAfter = $scope.chatMsg.substr(selection.end);
$scope.chatMsg = textBefore + name + textAfter;
}
$scope.updateChatMsg = function(chatMsg){
$scope.chatMsg = chatMsg;
}
$scope.sendChatMsg = function(){
var backend = $scope.convs[$scope.active.conv].backend.name;
$scope.view.addChatMsg($scope.active.conv, $scope.active.user, $scope.chatMsg,new Date().getTime()/1000, backend);
Chat[backend].on.sendChatMsg($scope.active.conv, $scope.chatMsg);
$scope.chatMsg = '';
};
然後一些HTML:
<div class="chat-msg-button" >
<button ng-click="view.toggle('emojiContainer')" ><img src="/apps/chat/img/emoji/smile.png"></button>
</div>
<form id="chat-msg-form" ng-submit="sendChatMsg()">
<div class="chat-msg-button" >
<button type="submit"><div class="icon-play"> </div></button>
</div>
<div id="chat-msg-input">
<textarea id="chat-msg-input-field" autocomplete="off" type="text" ng-model="chatMsg" ng-change="updateChatMsg(chatMsg)" placeholder="Chat message"></textarea>
<div>{{ chatMsg }}</div>
</div>
</form>
我想要實現:用戶鍵入的文本區域=>$scope.chatMsg
一些文字得到的值textarea。現在,用戶按下其中一個按鈕=>按鈕的名稱將添加到最新的光標位置。 (這是沒有問題找到最新的光標位置)
問題
還有就是$scope.chatMsg
,{{ chatMsg }}
的DIV中的價值,並在textarea的文本之間的差異。 textarea和div的內容始終保持不變。不過,按下按鈕的名稱添加到$scope.chatMsg
但textarea的內容沒有改變...
我怎樣才能解決這個問題是什麼時候? TIA
你可以放在一起的jsfiddle或plunker? – SoluableNonagon
http://plnkr.co/edit/lN7FulBZullRV0TG5y7T?p=preview即使沒有ng-change,它也可以很好地工作。我想我必須剝離下來我的代碼.... – LEDfan
你的按鈕包含在其中一個div,並:) jQuery的需要使用getSelection插件並不總是開槍點擊 – SoluableNonagon