Angular - 使用Post的routeProvider

时间:2016-08-09 18:58:12

标签: javascript angularjs

我在使用角度制作一个简单的页面,我在进行视频上传时,但在使用ng-view时,我无法再使用我创建的POST方法。 我应该在routeProvider中做什么才能使POST正常运行?

以下是我的routeProvider和Controller代码

app.js:

angular.module('videoUpload', ['ngFileUpload', 'ngRoute']);

angular.module("videoUpload").config(function ($routeProvider) {
    $routeProvider.when("/videolist", {
        templateUrl: "/views/videolist.html",
    });
    $routeProvider.when("/videoinsert", {
        templateUrl: "/views/videoinsert.html",
        resolve: {

        }
    });
});

控制器:

angular.module("videoUpload").
controller('videoUploadController', ['$http', 'Upload', '$scope', function($http, Upload, $scope){

  $http.get('/public/videos').then(function(response){
    console.log(response.data);
    $scope.videos = response.data;
  });

  $scope.submit = function(){
    Upload.upload({
      url: '/public/videos',
      method: 'post',
      data: $scope.upload
    }).then(function (response) {
      console.log(response.data);
      $scope.videos.push(response.data);
      $scope.upload = {};
    })
  }
}]);

我的帖子:

router.post('/videoinsert', video.single('file'), function (req, res, next) {
  console.log(req.body);
  console.log(req.file);
  var newUpload = {
    title: req.body.title,
    author: req.body.author,
    description: req.body.description,
    created_at: Date.now(),
    file: req.file
  };
  Upload.create(newUpload, function (err, next) {
    if (err) {
      next(err);
    } else {
      res.send(newUpload);
    }
  });
});

1 个答案:

答案 0 :(得分:0)

您未在Controller中提供routeProvider

试试这个:

angular.module("videoUpload").config(function ($routeProvider) {
    $routeProvider.when("/videolist", {
        templateUrl: "/views/videolist.html",
        //use controller here also,if required

    });
    $routeProvider.when("/videoinsert", {
        templateUrl: "/views/videoinsert.html",
        // assuming 'videoUploadController' is the controller used in this case
        controller : 'videoUploadController',
        resolve: {

        }
    });
});