2015-09-09 2 views
1

У меня есть этот HTML:AngularJs нг подать не может получить значение нг-модели

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

и в моем контроллер:

$scope.comment = ""; 

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

Он отлично работает, когда им печатать на машинке input модель обновляется и все. Но когда я нажимаю enter, я ожидаю, что значение с входа будет записано на консоль. Но, похоже, он не может получить обновленное значение от ng-model.

enter image description here

ответ

2

Попробуйте передать его в качестве аргумента:

<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); 
} 
+0

Nice спасибо. 1 вопрос. Как я могу снова ввести 'input'? – CENT1PEDE

+0

@ GreenFox Просто сделайте комментарий = ""; – Chrillewoodz

+0

Im делает '$ scope.comment =" "' внутри 'addComment()', но он не работает. :( – CENT1PEDE

1

кажется все правильно посмотреть.

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>

+0

Странно .. Это не работая для меня. – CENT1PEDE

Смежные вопросы