Hey there, I have a question that might seem simple to some of you. I'm struggling with fixing an error and I don't quite understand why it's happening :(
This error popped up while using Angular 1.x. What I need help with is creating an alert when the user clicks a button without entering a title in the form.
Let me share my code here, keeping it simple and easy to follow :D
index.html
<div class="form-group">
<label for="">Title</label>
<input type="text" ng-model="shipment_template.title" class="form-control">
</div>
<button ng-click="check">Check This</button>
Here's how I handled it in my controller:
shipmentTemplate.controller.js
if (typeof $scope.shipment_template.title == 'undefined') {
console.log("success")
} else {
alert("Please fill the title")
}
The issue arises when I enter something in the input field, I see success
in the console. However, if I leave it empty, I encounter this error instead.
Cannot read property 'title' of undefined
I initially tried using the length property instead of typeof, but unfortunately, it led to the same error :(
Apologies for any language errors in my explanation, and I would greatly appreciate your assistance in resolving this matter. Thanks! :-D