0

我正在尝试提交一个简单的联系表格。该表单位于引导模式内(不要认为这有任何区别),但控制器位于指令中。

表单的html如下:

<form name="contactForm" ng-submit="contactForm()" novalidate>
                <div class="form-group">
                    <div class="col-lg-6 col-md-6 col-sm-6">
                        <input type="text" placeholder="Full name" name="name" ng-minlength="3" max="20"
                               ng-model="name" id="name" class="form-control">
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6">
                        <input type="email" ng-minlength="4" placeholder="Email address" name="contactEmail"
                               ng-model="email" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-12 col-md-12 col-sm-12">
                        <input type="text" ng-model="subject" name="subject" ng-minlength="10" id=""
                               placeholder="Subject" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-12 col-md-12 col-sm-12">
                        <textarea class="form-control" ng-model="message" name="message" ng-minlength="10"
                                  placeholder="Your message"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="col-lg-12 col-md-12 col-sm-12">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <input type="submit" class="btn btn-primary" value="Submit" />
                    </div>
                </div>
            </form>

我认为这一切都很好。我的控制器的主体看起来像这样

    $rootScope.contactForm = contactForm();

function contactForm() {
  console.log('triggered!');
  var contactFormVars = {
    name: $rootScope.name,
    contactEmail: $rootScope.contactEmail,
    subject: $rootScope.subject,
    message: $rootScope.message
  }

  // With Promise
  Stamplay.Object('contactform')
    .save(contactFormVars)
    .then(function(res) {
      console.log('yes!');
    }, function(err) {
      console.log('No!');
    })

}

return directive;

};

编辑:控制器现在看起来像这样:

function contactForm($rootScope,$stamplay,$q) {
  $rootScope.data = {}

  $rootScope.data = {
    name: $rootScope.data.name,
    contactemail: $rootScope.data.contactemail,
    subject: $rootScope.data.subject,
    message: $rootScope.data.message
  }
  Stamplay.Object("contactform")
    .save($rootScope.data, function (err, res) {
      console.log(res);
      console.log(err);
      // res is the new car object
    });
}

当我单击提交按钮时,我收到以下错误,我一直在谷歌搜索

Error: v2.contactForm is not a function. (In 'v2.contactForm()',     'v2.contactForm' is an instance of FormController)
fn

对此的任何帮助表示赞赏。

编辑

好的,现在我已将 js 从指令中移出并将其放入主控制器。目前它没有任何区别,只是错误发生了非常轻微的变化:

angular.js:12722Error: v4.contactForm is not a function. (In 'v4.contactForm(ensureSafeObject(v5,text))', 'v4.contactForm' is an instance of FormController)

fn

不知道v2和v4有什么区别。

任何通过此阻止程序的建议表示赞赏。

4

0 回答 0