ng-model是未定义的,如果使用ng-if则可以使用ng-show [英] ng-model is undefined wih ng-if but working with ng-show

查看:48
本文介绍了ng-model是未定义的,如果使用ng-if则可以使用ng-show的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有非常基本的功能,例如在单击功能上获取文本值.我在ng-if中有一个文本框.但是,当我尝试获取该值时,它说" uneifned ",我对此大加赞赏,并知道ng-if创建了自己的作用域.因此,如果为true,则说明控制器中定义的单击功能为何起作用.两者都在控制器中定义. plnkr

I have very basic functionality which is like get text value on click function. I have a text box inside ng-if. But when I am trying to get the value it says 'undeifned' I red about it and came to know that ng-if create its own scope. So if it is true then why click function is working which is defined in controller. Both are defined in controller. plnkr

app.controller('MainCtrl', function($scope) {
  $scope.comment="";
  $scope.showBox = function (){
    $scope.showTextBox = true;
  }
  $scope.getComment = function (){
    alert($scope.comment);
    $scope.showTextBox = false;
  }

});

推荐答案

ng-if创建新的子范围.ng-show没有.我猜$ scope.comment是在ng-if内部定义的.

ng-if creates a new child scope. ng-show does not. I'm guessing the $scope.comment is defined inside the ng-if.

如果您不想使用ng-show,则有时可以使用对象而不是简单变量来工作.例如:

If you don't want to use ng-show, sometime using an object instead of a simple variable will work. Ex:

$scope.comment= { c: "" };
alert($scope.comment.c);
<textarea ng-model="comment.c">

更新的plunkr: http://plnkr.co/edit/DewF13GWVIS8bHQIIAbC?p=preview

Updated plunkr: http://plnkr.co/edit/DewF13GWVIS8bHQIIAbC?p=preview

这篇关于ng-model是未定义的,如果使用ng-if则可以使用ng-show的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆