简体   繁体   中英

Pass Array Data to factory from controller in AngularJS

Is it possible to pass array data from controller to factory and extract data from factory to slim framework to insert into a database?

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

I'am new in AngularJS, can you give me a example? thank you.

Yes you can do it on the following way:

Controller:

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

Factory:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM