如何正确地使用AngularJS和Laravel 5向数据库添加新记录


How to add new record to database with AngularJS and Laravel 5 on right way

我找到了一个如何正确编写AngularJS的指南:https://github.com/johnpapa/angular-styleguide#factories

在Laravel中,我创建了POST路由/api/addnew,现在我需要将参数从Angular传递到Laravel。

这是我的表单:

<form id="1" class="dd animated slideInDown ng-pristine ng-valid">
    <textarea class="form-control" placeholder="Vas Komentar" id="rep_text_0"></textarea>
   <a href="javascript: void(0);" add-comment class="btn btn-default pull-right">Send</a>
</form>

和我的工厂:

angular.module('commentsApp')
.factory('commentFactory', dataService);
dataSerive.$inject();
function dataService() {
    var someValue = '';
    var service = {
        save: save,
        someValue: someValue
    };
    return service;
    ////////////
    function save() {
        /* */
    };
}

点击指令:

angular
    .module('commentsApp')
    .directive('addComment', addNewComment);
function addNewComment() {
    var directive = {
        link: link,
        restrict: 'A'
    };
    return directive;
    function link(scope, element, attrs) {
      /* */
    }
}

我卡住了如何从表单传递细节到Angular,然后用参数在/api/addnew上发出post请求?

是否有任何方法可以使用按钮上的click指令将参数发送到工厂?

我不认为是必要的指令点击…而是使用ng-click从控制器管理数据(模型)。

<div ng-controller="yourController">
    <form id="1" class="dd animated slideInDown ng-pristine ng-valid">
        <textarea class="form-control" placeholder="Vas Komentar" id="rep_text_0" ng-model="yourCommentModel"></textarea>
       <a href="#" class="btn btn-default pull-right" ng-click="sendText(yourCommentModel)">Send</a>
    </form>
</div>

在你的控制器中,你可以使用$http或者你自己的工厂。

yourApp.controller('yourController', function($scope, $http, yourFactory){
    $scope.sendText = function(data){
      //http code...
      //or yourFactory code..
    };
});