【问题标题】:Cannot type inside TextArea when using dynamic index for ng-model?为 ng-model 使用动态索引时无法在 TextArea 内输入?
【发布时间】:2014-12-25 05:21:12
【问题描述】:

这个问题真的很奇怪,这个例子再简单不过了:) TextArea 以某种方式被阻止。

html

<div ng-app> 
<div ng-controller="ContactController">
<div  ng-repeat="x in names">
    <textarea class="form-control" rows="5" ng-model="dynamicTextArea[x.ID]">SOS</textarea>{{x.ID}} Why you can't type inside the area???
</div> 
</div>
</div>

<textarea class="form-control" rows="5" ng-model="dynamicTextArea" >SOS</textarea>

javascript

 function ContactController($scope) {
     $scope.names = [
        {
        ID: '5'
         },
        {
        ID: '6'
        },
        {
        ID: '7'
        }
    ];

 }

我什至创造了一个小提琴......

http://jsfiddle.net/c93qm5Lj/1/

€ 这是我真正想做的其余代码,它可能会帮助那些被困在这几个小时的人。我猜语法杀了我。

html

<button ng-click="addCommentBox(x.ID)" type="button" class="btn btn-xs btn-default">Submit comment</button>

javascript

$scope.addCommentBox = function(myID) { 
                  alert($scope.dynamicTextArea[myID]);                   
}

【问题讨论】:

    标签: angularjs dynamic textarea angular-ngmodel


    【解决方案1】:

    好吧,我刚刚发现了我的愚蠢错误。

    $scope.dynamicTextArea = {};
    

    你必须初始化它们。 但是我可以发誓我之前确实尝试过,但无论如何它都不起作用O_O

    【讨论】:

    • 为什么要使用 x.ID 和 ng-model?
    • 我需要创建动态文本区域并能够获取我的 $scope 中的值。我想不出另一种解决方案。我用我想做的全部内容编辑了我的第一篇文章。
    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 2015-07-26
    相关资源
    最近更新 更多