Angular: how to pass $ scope variables to a Node.js. server

right now i have this form:

<form ng-submit = "submit()"ng-controller = "formCtrl">
                <input ng-model="formData.stickie" type="text" id="sticky_content" />
                <button type="submit" id="add_sticky" value="add a new stickie!">new sticky</button> 
        </form>

driven by this model:

app.controller('formCtrl',function($scope,$http){

        $scope.submit = function() {
                $http.post('/api/stickies', $scope.formData)
                                **** somehow assign data to something useable by the function below???******

                        })
                        .error(function(data){
                                console.log('Error: ' + data);
                        });
        };
});

and I want to be able to use the published data here:

app.post('/api/stickies',function(req,res){
        var test = formData.stickie;    //returns undefined for sticky and for formData.stickie
        db.run("INSERT INTO stickies (data) VALUES (?)", [ test ]);
});

So, in general, I'm trying to pass the $ scope.formData variable to my server.js file so that it can be used in my app.post function (and inserted in db).

Edit: updated code according to the answer below: currently getting `ReferenceError: formData is not defined when I submit the form.

+4
source share
1 answer

ng-model . ,

<input ng-model="stickie_text" type="text" id="sticky_content" />

$scope.stickie_text 'formCtrl'. , : , .

:

<form ng-submit="submit()" ng-controller="formCtrl">
    <input ng-model="stickie_text" type="text" id="sticky_content" />
    <button type="submit" id="add_sticky" value="add a new stickie!">new sticky</button> 
</form>

: stickie_text. - . , ($scope.stickie_text).

$scope.submit:

$scope.submit = function() {
    $http
        .post('/api/stickies', {what: to, put: here})
        .success(function(data){
            //what to do here
        })
        .error(function(data){
            console.log('Error: ' + data);
        });
};

, , :

  • ?
  • ?
  • ?

: - .

POST . , stickie ( , req.body.stickie ), , , : {stickie: $scope.stickie_text}. , stickie_text - , , , , var, . body parser .

, , :

app.post('/api/stickies',function(req,res){
    var test = req.body.stickie;
    db.run("INSERT INTO stickies (data) VALUES (?)", [ test ]);
});

, . - (res), nodejs (, res.write).

, :

$scope.submit = function() {
    $http
        .post('/api/stickies', {stickie: $scope.stickie_text})
        .success(function(data){
            //what to do here? it up to you and the data you write from server.
        })
        .error(function(data){
            console.log('Error: ' + data);
        });
};

.

- @Kousha - ( formData ), , , , ), :

  • ng-model, . , :

    ng-model="formData.stickie"
    

    , , nodejs. , .

  • $scope.formData {stickie: whatEverIsInTheInputField}. , ng-model="formDta.foo" , $scope.formData {stickie: whatEverIsInTheInputField, foo: otherFieldValue}.

  • $scope.formData http-:

    $scope.submit = function() {
        $http
            .post('/api/stickies', $scope.formData)
            .success(function(data){
                //what to do here? it up to you and the data you write from server.
            })
            .error(function(data){
                console.log('Error: ' + data);
            });
    };
    
  • formData $scope , stickie .

+8

All Articles