1

In the following code, when the data is saved, the console.log(user) is returning undefined. What is wrong here?

<input type="text" class="form-control" id="inputEmail" placeholder="Name" ng-model="user.name">
<a class="btn btn-link pull-right" ng-click="save(user)">Save</a> 

Controllers:

LControllers.controller( 'InstanceCtrl', ['$scope', 'Instance', 'User',
function ($scope, Instance , User) {
    $scope.user= '';
    $scope.save = function (user) {
        console.log(user)// undefined
        console.log(user.name)       
    };

JSFiddle: http://jsfiddle.net/U3pVM/15248/

3
  • user is undefined or console.log Commented Apr 28, 2015 at 11:36
  • 1
    try to put user instead of use.name inside ng-model Commented Apr 28, 2015 at 11:37
  • Is it undefined even after you typed something in the text field? Commented Apr 28, 2015 at 11:40

3 Answers 3

1

For me , it seems fine

http://jsfiddle.net/U3pVM/15248/ JSFiddle

after your fill some value in input box, code works fine. First time user is an empty string and hence user.name would be undefined.

And yes the right way to declare an object is

$scope.name = {};

not

$scope.name="";
Sign up to request clarification or add additional context in comments.

Comments

0

I wired up a quick jsFiddle and it seems fine: http://jsfiddle.net/tfqytchk/

Of course the first time you press save user is an emptry string and user.name is undefined (I think that's what you're reporting). If you want that to change that behavior then change $scope.user = ''; to $scope.user = { name: '' };

1 Comment

Your reason seem valid to me, and the downvote is gone. Great.
0

Do not declare object like this

      $scope.user= '';

Declare object like

      $scope.user= {};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.