[英]Passing element in ng-click
我需要在ng-click中傳遞一個元素作為參數。
<textarea id="chat-msg" name="chat-msg" class="p-r-40 chat-reply form-control"></textarea>
<img src="libs/converse/lib/emoji/smile.png" class="image-size" ng-click="insertEmotion(document.getElementById('chat-msg'))" ></img>
在這里,onclick可以正常工作,但是如何在ng-click中傳遞它呢?
考慮將document.getElementById
部分移到insertEmotion()
函數中。
像這樣的insertEmotion('chat-msg')
insertEmotion
,其中insertEmotion
將元素名稱作為其參數。
聽起來您可能正在嘗試在控制器中操縱DOM,但這並不是真正的“角度方式”。 更好的方法可能是嘗試將textarea
綁定到模型變量,並讓ngClick
指令更新模型變量。
例如:
<textarea ng-model="chat"></textarea>
<img src="libs/converse/lib/emoji/smile.png" class="image-size" ng-click="chat = chat + ':-)'" ></img>
這是一個說明問題的插件: http ://plnkr.co/edit/d0hMVbrwKy6nBgVxWBQP?p=preview
也許嘗試創建聊天對象並將模型分配給這樣的視圖?
var app = angular.module('emotionApp',[]);
app.controller('EmotionCtrl',function($scope) {
$scope.chat = 'Chat message';
$scope.insertEmotion = function(emotion) {
$scope.chat = $scope.chat + emotion;
};
});
<div ng-app="emotionApp" ng-controller="EmotionCtrl">
<textarea ng-model="chat"></textarea>
<hr>
<button ng-click="insertEmotion(':)')">Insert</button>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.