简体   繁体   中英

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'. 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? You need it initialized first to be accessible from $scope .

$scope.user = {};

As user is not available it shows undefined when reading property of it. Just add following:

$scope.user = {};

in your controller.

Assign user = {} on ng-init

like

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

But other answers are also correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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