2015-10-14 119 views
0

是否可以將數組數據從控制器傳遞到工廠,並將數據從工廠提取到超薄框架以插入到數據庫中?從AngularJS的控制器傳遞數組數據到工廠

Array[{}] CONTROLLER -> FACTORY -> SLIM PHP FRAMEWORK -> DATABASE 

我是AngularJS的新手,你能舉個例子嗎?謝謝。

回答

0

是的,你可以把它做的方式如下:

控制器:

app.controller('fooCtrl', ['$scope', 'yourFactory', function($scope, yourFactory) { 
    $scope.arr; //your array 
    yourFactory.sendData($scope.arr).then(function(data) { 
     //handle after data is submitted to api/server 
    }) 
}]) 

廠:

app.factory('yourFactory', ['$http', function($http) { 
    return { 
     sendData : sendData 
    } 

    function sendData(arr) { 
     $http.post('<api_url>', arr).success(function(response) { 
      return response; 
     }) 
    } 
}) 
+0

謝謝!有用! :)但我有更多的問題在PHP中。 –

+0

對不起,不知道PHP;) –

+0

我不能返回數組中的數據數量我使用$ app = Slim :: getInstance(); \t $ app-> response() - > header(「Content-Type」,「application/json」); \t $ post = json_decode($ app-> request() - > getBody()); \t $ data = get_object_vars($ post); \t $ a = count($ post); \t echo json_encode($ a); –

相關問題