无法在 angularjs 中获取 textarea 值 [英] Cannot get textarea value in angularjs
问题描述
这是我的 plnkr:http://plnkr.co/edit/n8cRXwIpHJw3jUpL8PX5?p=preview你必须单击 li 元素,表单才会出现.输入一个随机字符串并点击添加通知".你会得到 undefined 而不是 textarea 文本.
标记:
<li ng-repeat="票中的票" ng-click="select(ticket)">{{ticket.text }}
<div ui-if="selectedTicket != null"><form ng-submit="createNotice(selectedTicket)"><textarea ng-model="noticeText"></textarea><button type="submit">添加通知</button></表单>
JS部分:
$scope.createNotice = function(ticket){警报($scope.noticeText);}
返回未定义".我注意到这在使用 angular-ui 的 ui-if 时不起作用.任何想法为什么这不起作用?如何解决?
您的问题出在 ui-if 部分.Angular-ui 为该指令中的任何内容创建一个新范围,因此为了访问父范围,您必须执行以下操作:
代替
Here is my plnkr: http://plnkr.co/edit/n8cRXwIpHJw3jUpL8PX5?p=preview You have to click on a li element and the form will appear. Enter a random string and hit 'add notice'. Instead of the textarea text you will get undefined.
Markup:
<ul>
<li ng-repeat="ticket in tickets" ng-click="select(ticket)">
{{ ticket.text }}
</li>
</ul>
<div ui-if="selectedTicket != null">
<form ng-submit="createNotice(selectedTicket)">
<textarea ng-model="noticeText"></textarea>
<button type="submit">add notice</button>
</form>
</div>
JS part:
$scope.createNotice = function(ticket){
alert($scope.noticeText);
}
returns 'undefined'. I noticed that this does not work when using ui-if of angular-ui. Any ideas why this does not work? How to fix it?
Your problem lies in the ui-if part. Angular-ui creates a new scope for anything within that directive so in order to access the parent scope, you must do something like this:
<textarea ng-model="$parent.noticeText"></textarea>
Instead of
<textarea ng-model="noticeText"></textarea>
这篇关于无法在 angularjs 中获取 textarea 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!