【问题标题】:Update page after form submit - using Angular表单提交后更新页面 - 使用 Angular
【发布时间】:2016-10-02 20:28:56
【问题描述】:

我正在使用 MEAN 框架 - 我有一个基本表单(如下所示),当输入数据时,它会被发送到一个 rest API,该 API 然后有一个使用 Mongoose 保存数据的函数。这一切都很好......但是我坚持更基本的东西!

用户提交此表单后,它会出现在带有 api/img/add 的空白页面上,我如何返回到我的原始页面?我尝试在表单标签中添加 ng-submit="fetchImages()" 然后在脚本中实现一个函数(也如下所示)但由于某种原因这不起作用,我是否错过了重点并且做错了什么?

提前致谢

<form action="api/img/add" method="post" enctype="multipart/form-data">
      <div>
        <label for="image">Select an image</label>
        <input type="file" name="image" id="image">
      </div>
      <div>
        <label for="title">Title</label>
        <input type="text" name="title" id="title">
      </div>
      <input type="submit">
    </form>

 < script >
   angular.module('app', []).controller('main', ['$scope', '$http',
     function($scope, $http) {
       $scope.images = [];
       $scope.fetchImages = function() {
         $scope.images = [];
         $http.get('api/img').then(function(res) {
           $scope.images = JSON.parse(res.data);
         }, function(res) {
           console.log(res.statusText);
         });
       }

       $scope.fetchImages();
     }
   ]); < /script>

【问题讨论】:

  • 如果你想通过$http提交,不要设置action。话虽如此,您需要使用FormData api 来使用ajax 上传图像,并且需要告诉$http 不要处理数据。建议寻找教程angular upload 或类似搜索

标签: javascript html angularjs node.js mean-stack


【解决方案1】:

如果你真的想回到最后一页,你可以使用:

$window.history.back();

在您的示例中,我将在控制器中创建如下所述的函数,然后进行更改

<input type="submit">

<input type="submit" ng-click="goHome()">

我在这里创建了一个超级简单的 plunk,带有一个可以带您返回的按钮:

https://plnkr.co/edit/wzMlPF9kOmrGg01mOnBB?p=preview

JS

app.controller('ctrl',function($scope,$window){

  $scope.goHome = function() {
    $window.history.back();
  }

});

HTML

<button ng-click="goHome()">Go Home</button>

【讨论】:

  • 我应该调用函数goBack(),但我累了:(
【解决方案2】:

试试这个: 在html中

    <form ng-submit="submitData()">
      <div>
        <label for="image">Select an image</label>
        <input type="file" ng-model="formdata.image" id="image">
      </div>
      <div>
        <label for="title">Title</label>
        <input type="text" ng-model="formdata.title" id="title">
      </div>
      <input type="submit">
    </form>

在您的控制器中:

 angular.module('app', []).controller('main', ['$scope', '$http',
 function($scope, $http) {

   $scope.formdata = {};
   $scope.images = [];

   $scope.fetchImages = function() {
     $scope.images = [];
     $http.get('api/img').then(function(res) {
       $scope.images = JSON.parse(res.data);
     }, function(res) {
       console.log(res.statusText);
     });
   }

   $scope.fetchImages();

   //this function will post data to your api without refreshing the page
   $scope.submitData = function(){

      $http.post('api-comes-here', $scope.formdata).then(function(res) {
          //handle success
      }, function(error) {
           //handle error
      });


   }
 }

【讨论】:

  • 谢谢这真的很有帮助,问题是我不太确定当我访问 API 时 formData 发生了什么。所以我的 API 看起来像这样: app.post('/api/img/add', upload.single('formData.image'), imgController.addImage());然后使用控制器中的功能,该功能基本上只使用猫鼬来保存图像。问题是 formData 似乎不在 req 中??
  • 试试这个: $http.post("api-comes-here", $scope.formdata).success(function(data, status) { }) 还要检查开发者工具中的网络标签查看请求是否与数据一起到达正确的 URL
  • 我检查了网络选项卡,可以看到它调用了正确的 API,但它似乎没有在 req 中传递任何数据,因为它正确地将条目保存到 mongo 但没有数据.
  • 好的,那么您可以在标题下的网络选项卡下查看请求有效负载的内容吗?如果它为空白,那么我们有问题,如果没有,那么您需要检查 API 端点来修复它..
  • 感谢您的帮助,我很感激。我正在使用 chrome 开发人员工具,检查过网络但看不到有效负载?似乎没有任何东西被传回,formdata应该在req中吗?
猜你喜欢
  • 2013-10-22
  • 1970-01-01
  • 2014-03-09
  • 2012-05-25
  • 1970-01-01
  • 2013-09-21
相关资源
最近更新 更多