[英]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上發出發布請求?
是否可以在按鈕上使用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..
};
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.