gpt4 book ai didi

node.js - MeanJs:如何发布数据?为什么服务操作不触发?

转载 作者:搜寻专家 更新时间:2023-10-31 23:47:33 27 4
gpt4 key购买 nike

我是 Angular 和 Express 的新手,MeanJs 所以需要帮助。我做错了什么,如何发布数据并得到答案?我想执行 Complile 函数并将代码从文本框传递到服务器并对其进行处理并获得答案。但是如何在lab.client.service.js或其他文件中编写代码呢?
因为我没有得到从文件 [3] 开始执行代码的任何信息。我使用 https://www.youtube.com/watch?v=DOqLbJTvCv8&feature=youtu.be 编写了这段代码教程。

文件

  1. lab.client.view.html 我有绑定(bind)到 data.code 角度对象的文本框

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

在 :

中有两个带触发功能的按钮
<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);
    };

最佳答案

您的代码有两个问题:

  1. 错误定义的自定义资源操作。查看argument list .第二个参数用于默认参数:

    return $resource('lab', {}, {  // <-- 3rd argument                          
    compile: {
    method: 'POST',
    url: '/lab/compile'
    },
    compileAndUpload: {
    method: 'POST',
    url: '/lab/compileAndUpload',
    }
    });
  2. $scope.message = Lab.compile 中,您正在分配操作而不是调用它:

    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();
    };

关于node.js - MeanJs:如何发布数据?为什么服务操作不触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32725138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com