简体   繁体   English

如何将参数从nodejs传递给angularjs控制器?

[英]how to pass parameter from nodejs to angularjs controller?

I have this in nodejs 我在nodejs中有这个

res.render('pages/dist/reset', {token:req.params.token});

and i can read it in reset.mustache 我可以在reset.mustache中阅读

<body  ng-app="eyeApp" ng-controller="ResetController">  
        <div id="wrapper">
            <div id="layout-static">
                <div class="static-content-wrapper">
                    <div class="static-content">                                                                                  

                        <div id="wrap" ui-view class="mainview-animation animated"></div>

                         <!--wrap -->     
                    </div>
                    <footer role="contentinfo" ng-show="!layoutLoading" ng-cloak>   
                        <div class="clearfix">

                            <button class="pull-right btn btn-default toUp btn-sm hidden-print" back-to-top style="padding: 1px 10px;"><i class="fa fa-angle-up"></i></button>
                        </div>   
                    </footer>
                </div>
            </div>
        </div>
     {{token}}

    </body>

Controller from this file is ResetController. 该文件中的控制器是ResetController。

ResetController: ResetController:

 angular
      .module('telcoserv.eye.reset', [
        'telcoserv.core.services'      
      ])
      .controller('ResetController', ['$scope', '$theme','$http','$state','$window','$stateParams', function($scope,$theme,$http,$state,$window,$stateParams) {
        'use strict';          

        $scope.submit = function(){             
            alert('123');                  
            alert($scope.token);
    }

      }]);

alert($scope.token) is undefined. alert($ scope.token)未定义。 when i say {{token}} in reset.mustache i can read value but $scope.token i can not read in resetController. 当我在reset.mustache中说{{token}}时,我可以读取值,但是$ scope.token我无法在resetController中读取。 Why?? 为什么??

$scope.submit = function(){             
    alert('123');                  
    alert($scope.token);

    $http({
        method:'',
        data: {json : data}
        url: ''
    }).then(function(response) {
        //Success response
    }, function(error) {
        //Failed response
    });
}

use $http to call your api. 使用$http调用您的api。 in method you can put GET, POST, PUT, DELETE, OPTIONS according to your api. method您可以根据您的api设置GET, POST, PUT, DELETE, OPTIONS

put your URL . 把你的URL

Put your request body in data as JSON only applicable for PUT, POST, DELETE. 将请求主体作为JSON放入data ,仅适用于PUT,POST,DELETE。

If you don't want to initialize some ways to achieve this and get that as parameter 1. Create a constant service for getting token and inject it as a parameter in your controller . 如果您不想初始化某些方法来实现并将其作为参数,请执行以下操作:1.创建一个用于获取令牌的常量服务,并将其作为参数注入到控制器中。 you can refer to this article how to create the constant service https://lostechies.com/gabrielschenker/2014/01/14/angularjspart-9-values-and-constants/ 您可以参考本文如何创建恒定服务https://lostechies.com/gabrielschenker/2014/01/14/angularjspart-9-values-and-constants/

2.On the route definition of the app , make use of the resolve in the route of the page using this controller and use of the same parameter and inject it as a dependency in your controller. 2.在应用程序的路由定义中,使用此控制器在页面的路由中使用解析,并使用相同的参数,并将其作为依赖项注入到控制器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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