2015-09-26 123 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

相關問題