AngularJS - Simple $scope console.log() returns undefined

前端 未结 2 761
说谎
说谎 2021-01-11 20:48

I\'m trying to make a simple console.log() from this $scope:

相关标签:
2条回答
  • 2021-01-11 21:33

    Because your JS code

    console.log($scope.customer);
    

    run when you init the CustomerController controller, at that time $scope.customer has no value and it return undefined.

    0 讨论(0)
  • 2021-01-11 21:49

    As the others have said, you need to initialize the customer object.

    Since there is no value of customer set from controller, it is appearing as undefined in the view. When you enter values in the input boxes, this will no longer be undefined, but since logging is done only once initially, typing values in input box has no effect

    Plunker Demo

    Here is the part which I have changed in script.js

    lima3app.controller("CustomerController", function($scope){
    
    $scope.customer = {
      address1 : 'address1',
      address2 : 'address2',
      city:'city'
    }
    
    console.log($scope.customer);
    
    });
    
    0 讨论(0)
提交回复
热议问题