0

I have a directive where the model is not updating like I think it is supposed to. Here is my html:

      <div class="text-area-container">
        <textarea ng-model="chatText" ng-keyup="updateCount(chatText)"></textarea>
      </div>
     <input ng-disabled="disableCommentButton()" value="Comment" ng-click="addMessage(chatText)"/>

As you can see I have a ng-model attached to the <textarea>. I don't know if this affects anything, but I also have a ng-keyup attached to the element, that takes the model and looks at how many characters are in it. The last piece is I have a ng-disable on an input that is evaluated by a function. Here is my the link function of my directive:

  link: function(scope) {
    scope.chatText = '';

    scope.countRemaining = 500;

    scope.updateCount = function(chatText) {
      scope.chatText = chatText;
      scope.countRemaining = scope.maxChatCount - chatText.length;
    };

    scope.disableCommentButton = function() {
      return _.isUndefined(scope.encounter) || _.isEmpty(scope.chatText);
    };
  }

The problem is that my scope.chatText is always undefined in the disableCommentButton method. I thought that by attaching a model to an element in the html, the I would have access to it on the scope. What am I missing here?

1 Answer 1

1

Try...

scope.disableCommentButton = function() {
  return _.isUndefined(this.encounter) || _.isEmpty(this.chatText);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why did that work? scope.encounter was working, but scope.chatText was not working. I don't know if scope.chatText not being defined earlier in the file had anything to do with it.
with scope being a global variable at the time of your function declaration it was referring to the out-dated scope. referring to this within the function refers to the new scope object.
So the scope changes? When does the scope become out of date?
scope is simply a container object. In your code you are changing scope when you add the encounter and chatText properties. So, really scope can change at anytime based on how your code changes it.

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.