简体   繁体   English

AngularJS无法读取未定义的属性

[英]AngularJS Cannot read property of undefined

I am trying to get a date value. 我正在尝试获取日期值。 Whenever the the checkbox is unchecked and the date picker is invisible I am getting the error that : 'Cannot read property 'NTLI' of undefined'. 每当未选中该复选框且日期选择器不可见时,我都会收到错误消息:“无法读取未定义的属性'NTLI'”。 if the check box is checked and the date picker is visible everything works fine 如果选中该复选框并且可见日期选择器,则一切正常

<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
}

You need to have user defined, 您需要由用户定义,

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

What is initial value if your user object? 如果您的user对象是什么,初始值是多少? You need it initialized first to be accessible from $scope . 您首先需要对其进行初始化才能从$scope进行访问。

$scope.user = {};

As user is not available it shows undefined when reading property of it. 由于user不可用,因此在读取其属性时显示undefined Just add following: 只需添加以下内容:

$scope.user = {};

in your controller. 在您的控制器中。

Assign user = {} on ng-init ng-init上分配user = {}

like 喜欢

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

But other answers are also correct. 但是其他答案也是正确的。

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

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