0

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..

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
Alex
  • 1,982
  • 4
  • 37
  • 70

1 Answers1

1

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.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • It works! Can you please elaborate in a few words? I've never seen this kind of use of `ng-model` before. – Alex May 30 '17 at 22:11
  • 1
    Basically you should never use a primitive type in two-way binding. Always use a object. From [this answer](https://stackoverflow.com/a/18128502/6835358): _If you two-way bind to a primitive (such as a Boolean in your case), the setter will set it on the current scope rather than the scope on which it is defined, which can cause a headache when you have a large user-interface with a lot of child scopes._ Another excellent explanation can be found in [this video](https://egghead.io/lessons/angularjs-the-dot). – Bruno Peres May 30 '17 at 22:26