我这里有一个简单的代码,想要将数据传递给laravel控制器,这样我就可以将它存储到数据库中。我的代码是:
AccountController.php
public function store(Request $request)
{
Account::create(array(
'email' => $request->get('email'),
'name' => $request->get('text'),
'age' => $request->get('age'),
));
return ['success' => true];
}
刀片
<form ng-submit="newAccount()">
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" ng-model="accountData.email">
</div>
<div class="form-group">
<label for="fullname">Full Name</label>
<input type="email" class="form-control" id="fullname" ng-model="accountData.name">
</div>
<div class="form-group">
<label for="age">age</label>
<input type="email" class="form-control" id="age" ng-model="accountData.age">
</div>
</form>
app.js
var app = angular.module('accountsApp', []);
app.controller('accountsCtrl', function($scope, $http) {
$scope.newAccount = function() {
//add data
$http.post('/api/accounts',
{
//what will I put here to get the textbox values?
}).
.success(function(response) {
scope.accounts = response;
})
.error(function(response) {
console.log(response);
});
};
});
正如您在app.js
中看到的那样,我一直坚持如何从我的刀片中的文本框中获取数据。是否有捷径可寻?谢谢。
答案 0 :(得分:1)
非常简单,您只需添加一个对象即可通过POST请求传递。 Laravel将从1比1中选择这些变量。
var app = angular.module('accountsApp', []);
app.controller('accountsCtrl', function($scope, $http) {
$scope.newAccount = function() {
//add data
$http.post('/api/accounts',
{
email: $scope.accountData.email,
text: $scope.accountData.text,
age: $scope.accountData.age
}).
.success(function(response) {
scope.accounts = response;
})
.error(function(response) {
console.log(response);
});
};
});