繁体   English   中英

AngularJS无法读取未定义的属性

[英]AngularJS Cannot read property of undefined

我正在尝试获取日期值。 每当未选中该复选框且日期选择器不可见时,我都会收到错误消息:“无法读取未定义的属性'NTLI'”。 如果选中该复选框并且可见日期选择器,则一切正常

<md-checkbox ng-model="user.NTLI" layout="row" ng-disabled="userForm.$invalid">
  NTLI
</md-checkbox>
<div ng-show="user.NTLI">
<fieldset class="standard">
    <legend>NTLI</legend>
    <md-input-container>
        <label>Efective date</label>
        <md-datepicker ng-model="user.efectiveDateNTLI"></md-datepicker>
    </md-input-container>
</fieldset>
</div>
var efDate = '';
if ($scope.user.NTLI != undefined) 
{
    efDate = $scope.user.efectiveDateNTLI
}

您需要由用户定义,

$scope.user ={};
if ($scope.user.NTLI != undefined) 
{
    efDate = $scope.user.efectiveDateNTLI
}

如果您的user对象是什么,初始值是多少? 您首先需要对其进行初始化才能从$scope进行访问。

$scope.user = {};

由于user不可用,因此在读取其属性时显示undefined 只需添加以下内容:

$scope.user = {};

在您的控制器中。

ng-init上分配user = {}

喜欢

<div ng-init="user = {}"> 
...
..//code
...
</div>

但是其他答案也是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM