vitalets / angular-xeditable

Edit in place for AngularJS
http://vitalets.github.io/angular-xeditable
MIT License
1.91k stars 403 forks source link

Xeditable form is saving form without even displaying it #482

Closed Arrow7000 closed 8 years ago

Arrow7000 commented 8 years ago

NOTE: I have copied this here from StackOverflow: http://stackoverflow.com/questions/37547037/xeditable-form-is-saving-form-without-even-displaying-it

I have followed the instructions exactly but my form is not working. I want to save a resource, but when I click the Edit button, which should display the form, it seems to skip the editing stage and immediately triggers the saveResource function - which should only happen when the form gets saved.

I've compared my code to the documentation again and again and can't work out what I am doing wrong.

HTML

  <form editable-form name="editResourceForm" onaftersave="saveResource()">
    <p>
      <strong editable-text="resource.title" e-name="title">
        {{resource.title}}
      </strong>
    </p>
    <div class="col-xs-12 col-sm-4">
      <button ng-click="editResourceForm.$show()" ng-show="!editResourceForm.$visible">Edit</button>
      <!-- buttons to submit / cancel form -->
      <span ng-show="editResourceForm.$visible">
        <button type="submit" class="btn btn-primary" ng-disabled="editResourceForm.$waiting">Save</button>
        <button type="button" class="btn btn-default" ng-disabled="editResourceForm.$waiting" ng-click="editResourceForm.$cancel()">Cancel</button>
      </span>
    </div>
  </form>

JS

app.controller('Ctrl', function($scope, $filter) {
  $scope.resource = {
    title: 'awesome resource'
  };

  $scope.saveResource = function() {
    console.log("Save resource");
  }

});

JSFIDDLE HERE

You can see that it is trying to save the form, because every time the Edit button is clicked, the console logs "Save resource". This should not happen when the edit button is clicked.

ckosloski commented 8 years ago

I think it's because your edit button does not specify a button type. By default, the button type is submit. So you are clicking on the button and it's submitting the form since it's a submit button. Try adding type="button" to your edit button.

Arrow7000 commented 8 years ago

Oh wow this actually works. Thanks @ckosloski! :) :+1: