0

I am trying to save the the text input on a textarea using AngularJS.

I need to save this each time the text is changed and store the values in a service so that I can retrieve it later to submit to the Database.

My textarea in my form.html looks as follows:

<textarea class="textarea" style="width: 90%; margin-left: 5%" rows="7" maxlength="100" ng-model="newMessage" placeholder="Additional comments"></textarea>

<!-- This isnt displaying anything -->
{{newMessage}}
<!-- This isnt displaying anything -->        
{{myTextArea}}

In my controller I implement a $watch() on the ng-model as below, but this is not working. I also tried to implement a ng-change but again with no success.

$scope.$watch("newMessage", function (newVal, oldVal) 
{
    console.log("Getting here..."); // Getting here

    if (newVal !== oldVal)
    {
        // This block is never executed
        $scope.myTextArea = "This is new values:  " + newVal;

        // Save the new textarea text to service   
        save($scope.myTextArea);
    }
});

$scope.save = function () 
{
    // This block never executed
    console.log("sharedProperties.setAdditionalCommentsText(): ");
    sharedProperties.setAdditionalCommentsText($scope.myTextArea);

    console.log("sharedProperties.getAdditionalCommentsText: " + sharedProperties.getAdditionalCommentsText());
};

And my service for saving the textarea text like below:

app.service('sharedProperties', function()
{
    // Initialise the Variables
    var additionalCommentsText = "";  

    return 
    {
        // Get and set the Additional Comments Text
        getAdditionalCommentsText: function()
        {
            return additionalCommentsText;
        },
        setAdditionalCommentsText: function(value)
        {
            additionalCommentsText = value;
        }
    }
}
6
  • 1
    do you define $scope.newMessage in your controller? it should be $scope.save($scope.myTextArea) not save($scope.myTextArea) in $scope.watch Commented May 10, 2016 at 8:13
  • No I had it defined but it didn't help either. Read on another SO post to remove it so hence it's not there anymore. Commented May 10, 2016 at 8:14
  • 1
    $scope.newMessage should be defined and the function assigned to $scope.save should take a param. Commented May 10, 2016 at 8:23
  • I defined the newMessage and changed the save as well as suggested but still no luck. The problem is that Im actually never even getting to call the save function in that block because the if (newVal !== oldVal) statement doesnt seem to get executed. Commented May 10, 2016 at 8:30
  • 1
    i try it myself and it works, pls check my answer to see if it help . Commented May 10, 2016 at 8:56

2 Answers 2

2

Try This, with ng-change

<textarea class="textarea" style="width: 90%; margin-left: 5%" rows="7" maxlength="100" ng-model="newMessage" ng-change="save(newMessage);" placeholder="Additional comments"></textarea>

JS

$scope.save = function (message) {
    console.log(message);
};
Sign up to request clarification or add additional context in comments.

Comments

1

I do it in my computer and it works,

in controller:

$scope.newMessage = '';  
$scope.$watch('newMessage', function(newValue, oldValue) {   
    if (newValue !== oldValue) {
        console.log('do save staff');
    }
});           

in html:

<textarea class="textarea" style="width: 90%; margin-left: 5%" rows="7" maxlength="100" ng-model="newMessage" placeholder="Additional comments"></textarea>

output:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.