简体   繁体   中英

how to pass parameter from nodejs to angularjs controller?

I have this in nodejs

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

and i can read it in 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:

 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. when i say {{token}} in reset.mustache i can read value but $scope.token i can not read in 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. in method you can put GET, POST, PUT, DELETE, OPTIONS according to your api.

put your URL .

Put your request body in data as JSON only applicable for 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 . you can refer to this article how to create the constant service 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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