Search code examples
angularjsangularjs-scopeangular-uiangularjs-ng-clickng-submit

ng-submit / ng-onclick only works first time


submit / ng-onclick only works once the first time i click it. I use the Ionic Framework (if this maybe helps someone) to create an app.

And im really new to this stuff...

The JS

.controller('CustomerCtrl', function($scope, $stateParams) {
    db.get($stateParams.customerId, function(err,doc) {
      $scope.customer = doc;
      //$scope.$apply();
    });

    $scope.doUpdate = function(customer) {
      db.put(customer, function(err, response) {
        if (!err) {
          console.log("Successfully Updated the Doc")
        }
        //$scope.formc.$setPristine(); // removes ng-dirty doesnt help
      });
    }

  })

The HTML

<div ng-controller="CustomerCtrl">
    <form name="formc" novalidate>    
        <div class="list">
          <label class="item item-input">
            <span class="input-label">Name</span>
            <input type="text" ng-model="customer.name">
          </label>
          <label class="item item-input">
            <span class="input-label">Adresse</span>
            <input type="text" ng-model="customer.address">
          </label>
        </div>
        <button type="submit" class="button button-large" ng-click="doUpdate(customer)">
          Submit
        </button>
    </form>
</div>

Solution

  • You can find an example in the angular api reference here. Does that help?

    The basic idea (taken from the link above): ngSubmit enables binding angular expressions to onsubmit events.

    <form ng-submit="submit()" ng-controller="ExampleController">
      Enter text and hit enter:
      <input type="text" ng-model="text" name="text" />
      <input type="submit" id="submit" value="Submit" />
      <pre>list={{list}}</pre>
    </form>