2015-09-26 124 views
0

我在这里有一个简单的代码,并希望将数据传递给laravel控制器,以便将其存储到数据库。我的代码是:将窗体数据传递给使用AngularJS的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

为什么不使用jquery这么简单的事情?你想通过Ajax或通过提交表单动作 – patricio

+1

发送给控制器,因为我想学习Angular @patricio – FewFlyBy

回答

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); 
     }); 

    }; 

}); 
+0

无法让它插入分贝,它的工作 – FewFlyBy

+0

然后,你可能有另一个可能,也许你'重新达到了Laravel的质量分配例外。检查控制台中的网络选项卡发生了什么。 – Pepijn

+0

我现在看到问题了,$ scope.accountData.email不会这样做。它一定是某种东西。我尝试了一个静态值,它的工作原理。例如电子邮件:'test' – FewFlyBy

相关问题