2

I have this HTML :

<form ng-submit="addComment()">
    <md-input-container flex>
        <label>Shoutout</label>
        <input type="text" ng-model="comment">
    </md-input-container>
</form>

and in my Controller :

$scope.comment = "";

$scope.addComment = function(){
    console.log("Value from input", $scope.comment);
    $scope.comment = "test"
    console.log("New Value", $scope.comment);
}

It works fine when im typing on the input the model is updating and all. But when I press enter I expect the value from the input to be logged on the console. But it seems that it cannot get the updated value from the ng-model.

enter image description here

jofftiquez
  • 7,548
  • 10
  • 67
  • 121

2 Answers2

3

Try passing it as an argument:

<form ng-submit="addComment(comment)">
  <md-input-container flex>
    <label>Shoutout</label>
    <input type="text" ng-model="comment">
  </md-input-container>
</form>

$scope.addComment = function(comment){
  console.log("Value from input", comment);
  $scope.comment = "test";
  console.log("New Value", comment);
}
Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
1

It seems all correct have a look.

angular.module('app', []).controller('MyController', ['$scope',
  function($scope) {

    $scope.comment = "";

    $scope.addComment = function() {
      console.log("Value from input", $scope.comment);
      $scope.comment = "test"
      console.log("New Value", $scope.comment);
    }

  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyController">
  <form ng-submit="addComment()">
    <md-input-container flex>
      <label>Shoutout</label>
      <input type="text" ng-model="comment">
    </md-input-container>
  </form>
  
  <p>{{"log-->> "+comment}}</p>
<div>
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44