forms - Angularjs cannot read property firstname of undefined -


i have form such as

html:

<form ng-submit="log(user)">    <input type="text" ng-model="user.firstname">  <input type="text" ng-model="user.lastname  </form> 

angular:

.controller ('', function ($scope){      $scope.log = function (user){          console.log (user.firstname)      } }) 

this working before throws cannot read property user.firstname

please me. thank you

angualarjs rich in 2 way data binding. didn't need pass scope object user in function log. available in controller updated value. initialise user scope object , try code

in html:

<form ng-submit="log()">    <input type="text" ng-model="user.firstname" />    <input type="text" ng-model="user.lastname" /> </form> 

in js

.controller ('', function ($scope){     $scope.user = {};    // declare here     $scope.log = function (){         console.log ($scope.user.firstname);      } }) 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -