0

This is my code.

$scope.sendFeedback = function ()  {

    var mobile1 = $scope.mobile;
    var feedback1 = $scope.feedback;

    var userfeedback = {
        mobile: mobile1,
        feedback: feedback1
    };

    var feedbackRef = firebase.database().ref("feedback/");
    var newFeedbackRef = feedbackRef.push();
    newFeedbackRef.set(userfeedback, function(error) {
        if (error) {
            alert ('Error while registering feedback.');
        } else {
            $scope.feedback = '';
            alert ('Your feedback is registered.');

        }
    });
}

After calling this function i can see alert ('Your feedback is registered.'); but $scope.feedback = '' not setting that particulat textField as empty. I tried $scope.feedback = null; also, but no luck.

what is wrong here?

Edit: HTML View added.

<md-input-container class="md-block">
    <label>Feedback</label>
    <input required="" name="feedback" ng-model="feedback" >
    <div ng-messages="projectForm.feedback.$error" role="alert">
     <div ng-message-exp="['required']">
        This field is required.
    </div>          </div>

</md-input-container>
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131

3 Answers3

1

I encountered that kind of problem several times, and solved it using the following workaround :

    angular.element(document.getElementById('doc'))
     .scope().$apply( function() {
            $scope.feedback=''
     )}

I made that very minimal working example, for the sake of testing the SO's snippet runner, and mostly using your own code. Hope this helps.

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>

<script>
  var module=angular.module('test',[]).controller('test', function($scope) 
{
    $scope.click = function() {
            $scope.feedback = '';
            alert ('Your feedback is registered.');
    }
})
</script>
</head>
<body>

<div ng-app="test" ng-controller="test" id='feedback'>
<md-input-container class="md-block">
    <label>Feedback</label>
    <input required="" name="feedback" ng-model="feedback" >
    <div ng-messages="projectForm.feedback.$error" role="alert">
     <div ng-message-exp="['required']">
        This field is required.
    </div>          </div>

   <button ng-click='click();'>click?</button>
</md-input-container>
  </div>
Flint
  • 1,651
  • 1
  • 19
  • 29
1
$scope.feedback = [];

(or)

setTimeout(function () {
    $scope.$apply(function () {
        $scope.feedback = [];
    });
}, 1000);
cezar
  • 11,616
  • 6
  • 48
  • 84
AngularJMK
  • 1,178
  • 13
  • 15
0

Do you have ng-app and ng-controller set? I tested your code on my machine, and it works as long as you have the app and controller set up. Can you make a JSFiddle?

Darin Cardin
  • 627
  • 1
  • 4
  • 10