0

I have a couple of drop downs and based on selection I updated the textarea content, but when I am trying to update the text the function, I presume gets triggered, and does not let me update.

Here is my html :

 <textarea  ng-model="desc" ng-change="des"></textarea>

and angular

somehow the code got deleted.. please visit plunker: http://plnkr.co/edit/pVSiNrnAOY0A5v0iTmYQ?p=preview

16
  • des() is a function not modal variable so you need to add bracket. Commented Mar 24, 2015 at 16:35
  • @Sam updated that and made it a function, it is still the same. Commented Mar 24, 2015 at 16:37
  • @TechnoCart that didn't work. Commented Mar 24, 2015 at 16:39
  • 1
    What you expect $scope.d to return? It has not been set, maybe you mean return $scope.d(), but this could cause an infinite loop, as the function updates $scope.desc which will trigger the ng-change... Commented Mar 24, 2015 at 16:48
  • 1
    @Sam $scope.d returns a string it has the same value as $scope.desc and $scope.dsc .. I was attempting different ways but couldn't figure... so it is a little redundant to use so many variables to just return same value. Commented Mar 24, 2015 at 16:58

3 Answers 3

1

It is better to use watch functions, especially if values might change in the controller, as this ensures that they stay in sync. This solution also seems easier to see what is going on: plunkr solution

Just using a watch function to watch the selected values:

 $scope.$watch('selectedId', function(newValue){
    $scope.d();
  });

I am not sure if you want the user to be able to change the value in the textarea or not. If you don't, like in your example, then I suggest that you add the attribute ng-disabled="true" to your text area.

Sign up to request clarification or add additional context in comments.

Comments

0

you also need

$scope.des = function() {
    return $scope.d()
  }

Not sure what your trying todo, but if have just plunked this

// Code goes here
app = angular.module('myapp',[]);
app.controller('main', function($scope) {
  $scope.desc = "";
  $scope.stat = "test";
  $scope.d = function() {
    for (var i = 0, len = $scope.stat.length; i < len; i++) {

      if ($scope.stat[i].Id == $scope.selectedId && $scope.statDate == $scope.stat[i].StatDate) {
        $scope.desc = $scope.stat[i].D;
        return $scope.stat[i].D;
      }
    }
    return "";
  };

  $scope.des = function() {
    return $scope.d()
  }
});

And I get your issue, if I comment out

// $scope.desc = $scope.stat[i].D;

it works, but I presume that bit you need :)

What are you trying todo? I think you are trying to change model as the view is changed. You might need a directive for this.

This is part of something I did for datetime stuff

.directive('formatteddate', function ($filter) {
    return {
        link: function (scope, element, attrs, ctrl) {
            ctrl.$parsers.unshift(function (viewValue) {

This is the plunk of your code/html.

http://plnkr.co/edit/3GjP7YVsKFIYAAJQwmOK?p=preview

11 Comments

it does if you comment out 'that' line of code, if you change the line of code. :)
this one // $scope.desc = $scope.stat[i].D; you can change this code to something that changes .desc and that works, but I don't understand what you are doing with this.
The reason why it's not working in this plunk is because $scope.stat and $scope.statDate and all that nonsense are all undefined. So that if statement always 100% of the time evaluates to true, so that bit of code is always run which resets the $scope.desc to an undefined variable. So you try to type and it instantly gets reset. I assume he is trying to change the value to some specific value when you type in a certain value. He did not provide the code for this.
@Steve Drake, $scope.selectedId & $scope.statDate are two drop downs to select from of json arrays, of 1-20 and the other "03/20/2015" and :"03/26/2015"...... the stat array is is like so: $scope.stat = ([{"Id":1,"D":"ABCD","StatDate":"03/20/2015"}, {"Id":2,"D":"ABCD" ,"StatDate":"03/26/2015"}]);
so, when you EDIT the textbox you want it to set to ABCD if Id=1 and the StartDate is correct? I have added // Fake dropdown $scope.selectedId =1; $scope.statDate = '03/20/2015'
|
0

okay,so what about if you just update the ng-model value of textarea to the value of your select dropdown.

Here's the plunker http://plnkr.co/edit/mXQTcBu5rOJaQJKEFKpm?p=preview

change the model name to some:

 <textarea ng-model="textarea" ng-change="desc()"></textarea>

and then update in, in your script where your code logic is:

$scope.d = function() {
    for (var i = 0, len = $scope.stat.length; i < len; i++) {
      console.log(i);
      if ($scope.stat[i].Id == $scope.selectedId && $scope.statDate == $scope.stat[i].StatDate) {
        $scope.desc = $scope.stat[i].D;
        **$scope.textarea=$scope.desc;**
        return $scope.stat[i].D;
      }
    }
    return "";
  };

1 Comment

Its working right,it is letting you modify the text area filed which wasn't happening before.

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.