Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数据从服务器传递到服务器_Javascript_Angularjs_Node.js - Fatal编程技术网

Javascript 将数据从服务器传递到服务器

Javascript 将数据从服务器传递到服务器,javascript,angularjs,node.js,Javascript,Angularjs,Node.js,对不起,我是这方面的新手 假设我在node express中有以下内容 router.get('/:name', function(req, res, next) { res.render("test", {test:req.test}); }); 如何获取角度以拾取测试变量 我试着在html页面中执行 <div ng-controller='UserCtrl'>{{ data }}</div> <script>

对不起,我是这方面的新手

假设我在node express中有以下内容

router.get('/:name', function(req, res, next) {
      res.render("test", {test:req.test});
});
如何获取角度以拾取测试变量

我试着在html页面中执行

    <div ng-controller='UserCtrl'>{{ data }}</div>
    <script>
         var test=!{JSON.stringify(test)};
    </script>
req.test如下所示

 [
   { 
      name: 'test' 
   }
 ]
“数据”每次都显示为空

感谢您在express res中的帮助。在我看来,json是,最好的选择是,任何传递给它的对象都会被转换成JSON,你可以像以前那样进行转换,但是render更适合html,在html中,你必须为客户端呈现一个漂亮的小视图,或者在发送JSON之前需要进行某种操作,或者JSON太大,你不想做任何修改您将路由器文件弄脏,并将其委托给视图文件。。else res.json是一种非常简洁的方法

修改控制器代码,如下所示:

productApp.controller('UserCtrl',['$scope', '$http' , function($scope, $http){
    $http.get('/test').then(
           function(response){
               $scope.data = test;
           },
           function(err){
               console.error(err);
           }); //'/test' <- this is the url; 
    //$scope.data=test;
}]);
并将其从代码中删除

<script>
    var test=!{JSON.stringify(test)};
</script>

如果希望与服务器通信,最常见的方法是通过AJAX请求。Angular有一整套服务来帮助您进行沟通

第一个示例几乎完全符合您的要求:

$http({
  method: 'GET',
  url: '/testName'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

它使用承诺作为回应。只需在适当的回调函数中添加您的操作,然后就可以开始了。

您需要使用来自angular脚本或服务的ajax调用,以获得快速js后端响应

您可以创建如下所示的服务-

productApp.service("API", function($http){

     this.callAPI = function(name){

         return $http({
            url : "localhost:3000/"+name, 
            data : "", // pass data if any here
            method: "get"
         });
     }; 

});
productApp.controller('UserCtrl',function UserCtrl($scope, API){
   //$scope.data=test;

   // call service api here 
   API.callAPI("some name").then(function(response){
       $scope.data = response;
     }, function(){ 
        // error goes here 
   });
});
然后,您需要将此服务注入控制器并按如下方式调用-

productApp.service("API", function($http){

     this.callAPI = function(name){

         return $http({
            url : "localhost:3000/"+name, 
            data : "", // pass data if any here
            method: "get"
         });
     }; 

});
productApp.controller('UserCtrl',function UserCtrl($scope, API){
   //$scope.data=test;

   // call service api here 
   API.callAPI("some name").then(function(response){
       $scope.data = response;
     }, function(){ 
        // error goes here 
   });
});
此外,您可以更改express js应用程序以发送JSON数据,如下所示-

res.json({...}); // instead of res.render(..);

我建议做一个$HTTP服务调用来接收这个变量,但是如果你真的希望这个变量/数据直接呈现在页面中,并且在控制器中可用,你应该考虑做如下:

在呈现/返回的HTML中:

<head>
...
<script>
    angular.module('myAppConfig', [])
        .value('MyTestConfig', !{JSON.stringify(test)});
</script>
</head>
<body ng-app="myApp">
...        
    <div ng-controller='UserCtrl'>{{ data }}</div>        
...
</body>
原始模态定义如下所示:

productApp.controller('UserCtrl', function UserCtrl($scope, MyTestConfig){
    $scope.data = MyTestConfig;
});
angular.module('myApp', ['myAppConfig']);

根据您配置视图的方式,您可能需要调整脚本标记的位置。这也只能在注入完成之前起作用,也就是说,这对部分不起作用。

而且您的后端语言是?抱歉,我使用的是node Expression。这里有很多问题。。你无法神奇地获得数据。。你必须请求它。@Minato我想知道我是否可以不在.get方法中使用res.json来完成这项任务。。没有res.jsontanks@minato就可以了,但最后我不得不使用res.json:-我想不可能全部都有。你可以做res.render。。您在express上使用哪种类型的模板引擎?Jade haml还是什么?我在使用ejs模板这里看看这个谢谢@hbillings,这个有效,和minato的一样,我尝试在服务器上使用这个没有res.json的,但是我没有工作,只有在我使用json时才工作,谢谢@Jaymes我尝试用这个脚本改变一切,但是没有工作,它返回一个空数据:-啊,我忘了问,您使用的模板语言是什么,因为这将更改.value部分。如果您使用EJS作为视图渲染器,请尝试代替!{JSON.stringifytest}谢谢@chandan我已经试过了,但似乎不起作用,一些名称部分,我试过输入参数名,例如luis,但仍然不起作用,我做错了什么?