Angular - 表格不会提交

时间:2016-01-26 17:48:52

标签: javascript angularjs

我似乎忽视了一些简单的事情,但它让我难过。

当我点击提交按钮时为什么没有发生?

<section ng-controller="SavingsController as savingsCTRL">
  <form name="createSavingForm" class="form-horizontal" novalidate>
    <fieldset>
      <!-- Title Box Start-->
      <div class="form-group new-deal-form" show-errors>
        <label for="title">Title</label>
        <input name="title" type="text" ng-model="savingsCTRL.title" id="title" class="form-control" placeholder="Title" required>
        <div class="sub-label">Enter the Title of the Deal.</div>
        <div ng-messages="savingForm.savingsCTRL.title.$error" role="alert">
          <p class="help-block error-text" ng-message="required">Saving title is required.</p>
        </div>
      </div>

      <!-- Title Box End-->

      <!--Submit Button Start-->

      <div class="form-group buttons-cancel-submit">
        <button class="btn btn-default " ng-click="savingsCTRL.cancel()">Cancel</button>
        <input type="submit" class="btn btn-success " ng-click="savingsCTRL.create(); submitForm(createSavingForm.$valid)" >
      </div>
    </fieldset>
  </form>
</div>
</div>
</section>
为了简单起见,我把大部分表格都拿走了,但还有什么不对呢?

节省控制器功能

 // Create new Saving
        $scope.create = function () {
            $scope.error = null;

            alert("create");

            // Create new Saving object
            var saving = new Savings({


                title: this.title,
                details: this.details,
                retailer: this.retailer,
                price: this.price,
                link: this.link,
                image: $scope.user.imageURL,
                urlimage: this.urlimage,
                tags: this.tags
                //startdate: this.startdate,
                //enddate: this.enddate


            });

            // Redirect after save
            saving.$save(function (response) {
                $location.path('savings/' + response._id);

                // Clear form fields
                $scope.title = '';
                $scope.details = '';
                $scope.retailer = '';
                $scope.price = '';
                $scope.link = '';
                $scope.image = '';
                $scope.urlimage = '';
                $scope.tags = '';


            }, function (errorResponse) {
                $scope.error = errorResponse.data.message;
            });
        };

2 个答案:

答案 0 :(得分:1)

主要问题是,您将controller as语法与$scope混合使用。

根据documentation,我们应该使用this代替$scope

  

...使用thisng-controller = "SettingsController1 as settings"

将方法和属性直接绑定到控制器上

比,submitForm不是预定义的方法,应该先在控制器中定义

this.submitForm = function(isValid){
   console.log('Submitting form: ' + isValid)
}

除此之外,将其与ng-submit= "savingsCTRL.submitForm(createSavingForm.$valid)"

绑定

使用工作代码查看Plunker。 (我使用了ng-click =“savingsCTRL.create()”,因为我们没有应用程序的所有部分)

答案 1 :(得分:0)

将表单提交事件绑定到ng-submit

示例:ng-submit="submitForm(createSavingForm.$valid)"

相关问题