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

4
  • Some error output? Commented May 30, 2017 at 21:58
  • No, nothing happens. Commented May 30, 2017 at 21:58
  • Category is not added too? Commented May 30, 2017 at 21:59
  • A new category is being added. Commented May 30, 2017 at 22:00

1 Answer 1

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.

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

2 Comments

It works! Can you please elaborate in a few words? I've never seen this kind of use of ng-model before.
Basically you should never use a primitive type in two-way binding. Always use a object. From this answer: 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.

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.