3

I am new to angular and express, MeanJs so need help. What I am doing wrong, how to post data and get answer? I want to execute Complile function and pass the code from text box to server and do something with it and get answer. But how to write code in lab.client.service.js or others files?
Becouse I do not get any information that starting from file [3] the code were executed. I wrote this code using https://www.youtube.com/watch?v=DOqLbJTvCv8&feature=youtu.be tutorial.

Files

  1. lab.client.view.html I have text box with binds to data.code angular object

    <textarea ng-model="data.code"></textarea>
    

and have two buttons with trigers functions in :

<div style="margin:12px;">
<button type="button" class="btn btn-primary" ng-click="LabController.compile()">Compile</button>
<button type="button" class="btn btn-primary" ng-click="LabController.compileAndUpload()">Cimpile & Upload</button>
</div>
  1. lab.client.controller.js

    'use strict';
    
    angular.module('core').controller('LabController', ['$scope', 'Lab',
    function($scope, Lab) {
        $scope.data = {
            code: ''
        };
    
        this.compile = function() {
                    console.log('User clicked compile', $scope.data.code);       //This message is printed in browser console.
            $scope.message = Lab.compile                                   //But this action does not executes???
        };
    
        this.compileAndUpload = function() {
                     console.log('User clicked compileAndUpload', $scope.data.code);    //This message is printed in browser console.
             $scope.message = Lab.compileAndUpload;             //But this action does not executes???
        };
    }
    ]);
    
  2. lab.client.service.js

    'use strict';
    
    angular.module('core').factory('Lab', ['$resource', function($resource) {
        console.info("client.service: Veikia!");
        return $resource('lab', {                           
            compile: {                     //I do not see that there URL were triggered
                method: 'POST',
                url: '/lab/compile'  
            },
            compileAndUpload: {   //I do not see that there URL were triggered
                method: 'POST',
                url: '/lab/compileAndUpload',               
            }
        });
    }
    ]); 
    
  3. lab.server.routes.js

    'use strict';
    
    module.exports = function(app) {
    var lab = require('../../app/controllers/lab.server.controller');
    
    // Lab Routes
    app.route('/lab/compile')
        .post(lab.compile);
    
    app.route('/lab/compileAndUpload')
        .post(lab.compileAndUpload);
    };
    
  4. lab.server.controller.js

    'use strict';
    
    var mongoose = require('mongoose'),
    _ = require('lodash');
    
    var response = {
    'message' : 'It works'
    };
    
    exports.compile = function(req, res) {
    var code = req.body.data.code;
    res.jsonp(response);
    };
    
    exports.compileAndUpload = function(req, res) { 
        var code = req.body.data.code; 
    res.jsonp(response);
    };
    
Drasius
  • 825
  • 1
  • 11
  • 26
  • 1
    call the compile function not assign the function `$scope.message = Lab.compile()` , – hassansin Sep 26 '15 at 05:06
  • I tried this. Then I get error when click button: TypeError: Lab.compile is not a function at compile (lab.client.controller.js:12) at Parser.functionCall (angular.js:10903) at angular-touch.js:441 at Scope.promises.$get.Scope.$eval (angular.js:12811) at Scope.promises.$get.Scope.$apply (angular.js:12909) at HTMLButtonElement. (angular-touch.js:440) at angular.js:2853 at forEach (angular.js:325) at HTMLButtonElement.eventHandler (angular.js:2852) – Drasius Sep 27 '15 at 07:04

1 Answers1

3

You have two issues with the code:

  1. Incorrectly defined custom resource actions. See the argument list. 2nd argument is for default Parameters:

    return $resource('lab', {}, {  // <-- 3rd argument                          
     compile: {                     
        method: 'POST',
        url: '/lab/compile'  
     },
     compileAndUpload: {
        method: 'POST',
        url: '/lab/compileAndUpload',               
     }
    });
    
  2. In $scope.message = Lab.compile you are assigning the action instead of invoking it:

    this.compile = function() {
         console.log('User clicked compile', $scope.data.code);       //This message is printed in browser console.
         $scope.message = Lab.compile() // <-- invoke
    };
    
    this.compileAndUpload = function() {
         console.log('User clicked compileAndUpload', $scope.data.code);    //This message is printed in browser console.
         $scope.message = Lab.compileAndUpload();
    };
    
hassansin
  • 16,918
  • 3
  • 43
  • 49
  • Hello, thanks, I acceot is as ansver as the request started working. Only one question.... How need to pass the text from the textBox to server in these "compile" and "compileAndUpload" actions? – Drasius Sep 27 '15 at 11:13
  • Please check the [doc](https://docs.angularjs.org/api/ngResource/service/$resource), there is some examples on how to post data. – hassansin Sep 27 '15 at 11:25