2

In the following example, message is undefined when I display it in the controller after the event is fired. Why?

<form>
  <input type="text" ng-model="message.Title" />
  <textarea ng-model="message.Content"></textarea>         
  <input type="submit" value="Send Message" ng-click="sendMessage(message)" />
</form>

Controller:

$scope.sendMessage = function(message) {
  console.log(message);
}

My code seems identical to the documentation here except my controller manages the entire "page" not just the form.

2 Answers 2

4

Wow nevermind, apparently when you submit with blank values it doesn't even create the object.

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

Comments

4

I see you've found your problem, but I'd like to propose a solution to prevent your problem anyway:

<form name="messageForm" ng-submit="sendMessage(message)">
  <input type="text" ng-model="message.Title" required/>
  <span ng-show="messageForm.title.$error.required && messageForm.title.$dirty">required</span><br/>
  <textarea ng-model="message.Content"></textarea>         
  <input type="submit" value="Send Message" ng-disabled="messageForm.$invalid" />
</form>

The above will make the Title required, display an error message, and disable your submit button if the form isn't valid.

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.