javascript - Cannot initialize ng-model in JS rather than HTML -


html:

... <input ng-init="item.check=false" type="checkbox" ng-model="item.check"> ... <input ng-init="item.name=''" class="form-control" ng-model="item.name"> ... 

js:

var myapp = angular.module('myapp', []); myapp.controller('appctrl', function ($scope, $http) { var refresh = function() {     $scope.item.check = false;     $scope.item.name = "";  } //call upon loading app initialize values, called upon events refresh field refresh(); 

i thought js sufficient initialize values , got rid of both checkbox , form's ng-init ended getting error:

typeerror: cannot set property 'name' of undefined 

the app works if leave both ng-init. works when remove ng-init 1 of 2 elements (so either checkbox or form) not work if remove both. going on here? doing correctly or there better way initialize?

thanks.

you can add $scope.item = {} in first line of controller.

javascript reports error when trying access $scope.item.name when $scope.item doesn't exist (or not object in cases).

when ng-init, angular creates object don't error.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -