使用AngularJs无法获得textarea值

时间:2021-10-08 20:07:10
<tr class="labels">
    <td nowrap="nowrap">Address</td>
    <td nowrap="nowrap"><label for="tbAddress"></label>
        <textarea name="tbAddress" id="tbAddress" cols="39" rows="5" ng-model="$parent.tbAddress"></textarea>

    </td>

</tr>
<tr>
    <td>&nbsp;</td>
    <td align="right">
        <input type="submit" name="button" id="button" value="Submit Record" class="btns" />
        <input type="reset" name="button2" id="button2" value="Cancel" class="btns" />
    </td>
</tr>

I'm not be able to get textarea value using AngularJS. All the input text fields are getting through the controller except the textarea. What am I doing wrong ?

我无法使用AngularJS获得textarea值。除textarea之外,所有输入文本字段都通过控制器。我究竟做错了什么 ?

alert($scope.tbAddress);
var thisData = {
    'name': $scope.tbFN,
    'company': $scope.tbCompany,
    'designation': $scope.tbDesignation,
    'email': $scope.tbEmail,
    'phone': $scope.tbPhone,
    'mobile': $scope.tbMobile,
    'address': $scope.tbAddress
};

It's alert undefined...

这是警报未定义...

1 个解决方案

#1


6  

If you have alert($scope.tbAddress); positioned right there, of course it'll end up as undefined. Nothing has been assigned to $scope.tbaddress at this point. (but maybe you have it defined earlier, but it's just not being shown in the code).

如果你有警报($ scope.tbAddress);就在那里,当然它最终会被定义为未定义的。此时没有为$ scope.tbaddress分配任何内容。 (但也许你之前已定义它,但它只是没有在代码中显示)。

I actually didn't have a problem getting it to work. I wrapped the alert in a function that is bound to the submit button. I'm able to get the value. See my example.

我实际上没有问题让它工作。我将警报包含在绑定到提交按钮的函数中。我能够获得价值。看看我的例子。

I also took the liberty of changing your $scope a bit. It's recommended that every ngModel has a . in it's name. So rather than tbAddress it should be tb.address. It has to do with how angular inheritance is handled.

我也冒昧地改变你的$ scope。建议每个ngModel都有一个。在它的名字。因此,而不是tbAddress,它应该是tb.address。它与角度继承的处理方式有关。

Refer to:

参考:

#1


6  

If you have alert($scope.tbAddress); positioned right there, of course it'll end up as undefined. Nothing has been assigned to $scope.tbaddress at this point. (but maybe you have it defined earlier, but it's just not being shown in the code).

如果你有警报($ scope.tbAddress);就在那里,当然它最终会被定义为未定义的。此时没有为$ scope.tbaddress分配任何内容。 (但也许你之前已定义它,但它只是没有在代码中显示)。

I actually didn't have a problem getting it to work. I wrapped the alert in a function that is bound to the submit button. I'm able to get the value. See my example.

我实际上没有问题让它工作。我将警报包含在绑定到提交按钮的函数中。我能够获得价值。看看我的例子。

I also took the liberty of changing your $scope a bit. It's recommended that every ngModel has a . in it's name. So rather than tbAddress it should be tb.address. It has to do with how angular inheritance is handled.

我也冒昧地改变你的$ scope。建议每个ngModel都有一个。在它的名字。因此,而不是tbAddress,它应该是tb.address。它与角度继承的处理方式有关。

Refer to:

参考: