Reputation: 2062
In my Ionic app I have a form with an input
field inside a form
, and a button
. Upon button
click some action should happen inside the controller
and the input
field should clear, but it does not happen. This seems like something really easy but I cant understand what it wrong. Please advise.
I read here, here and here, nothing helps.
The html:
<form novalidate name="customCategoriesForm">
<ion-list>
<ion-item>
<div class="item item-input item-stacked-label noBorder">
<span class="input-label">Create a new category</span>
<div class="catRow">
<input type="text"
id="newCategoryInput"
name="addNewCategory"
placeholder="Category name"
ng-model="addNewCategory"
ng-minlength="3"
required
>
<button ng-click="addCategory(addNewCategory)"
ng-disabled="!customCategoriesForm.addNewCategory.$valid"
class="button button-small button-outline button-positive catButton">Add</button>
</div>
</div>
</ion-item>
<ion-item>
...
</ion-item>
</ion-list>
</form>
The JS:
$scope.addCategory = function (newCategory) {
$scope.ExistingCategoriesArray.push(newCategory);
$scope.addNewCategory = "";
}
EDIT:
No error appears, nothing happens..
Upvotes: 0
Views: 1501
Reputation: 16365
To solve your problem change $scope.addNewCategory
to $scope.addNewCategory.value
, like this:
The .js:
$scope.addNewCategory = {value : ""}; // initialize a object addNewCategort with the property value = ''
$scope.addCategory = function (newCategory) {
$scope.ExistingCategoriesArray.push(newCategory);
$scope.addNewCategory.value = ""; // clear the property value of addNewCategory
}
The html:
<form novalidate name="customCategoriesForm">
<ion-list>
<ion-item>
<div class="item item-input item-stacked-label noBorder">
<span class="input-label">Create a new category</span>
<div class="catRow">
<input type="text"
id="newCategoryInput"
name="addNewCategory.value" <!-- Edit this line -->
placeholder="Category name"
ng-model="addNewCategory"
ng-minlength="3"
required
>
<button ng-click="addCategory(addNewCategory.value)" <!-- and this line -->
ng-disabled="!customCategoriesForm.addNewCategory.$valid"
class="button button-small button-outline button-positive catButton">Add</button>
</div>
</div>
</ion-item>
<ion-item>
...
</ion-item>
</ion-list>
</form>
You always need to use a dot in ngModel. Read this reference.
Upvotes: 1