简体   繁体   English

将NodeJS函数传递给Angular指令。

[英]Pass NodeJS function to an Angular Directive.

I am currently trying to display a error message if a user inputs a wrong username or password. 如果用户输入错误的用户名或密码,我目前正在尝试显示错误消息。 The tricky part is that I am not using EJS or Handlebars, or any kind of template engine but raw HTML with Bootstrap. 棘手的部分是我没有使用EJS或Handlebars或任何类型的模板引擎,而是使用带有Bootstrap的原始HTML。

I saw one other good question Good Similar Question of how to pass values from Nodejs to Angular. 我看到了另一个很好的问题 ,关于如何将值从Nodejs传递到Angular的类似问题 Sadly this didn't seem to help. 可悲的是,这似乎没有帮助。

Here is my NodeJS + Passport User Authentication function: 这是我的NodeJS + Passport用户身份验证功能:

 function isLoggedIn(req, res, next) {
        console.log('here is Authenticated', req.isAuthenticated());
        if (req.isAuthenticated()){
           return next();
        }else{
           res.json({"message": "Failed login. Wrong Username or Password"});
            res.redirect('/login');
            console.log("NO PERMISSION HAS BEEN GIVEN");
        }
    }

So my Idea was to send the message as an JSON object and get it with Angular: 所以我的想法是将消息作为JSON对象发送并通过Angular get

 var app = angular.module('myApp', []);
        app.controller('myCtrl', function($scope, $http) {

            $http.get("/login").then(function(data){
                $scope.message = data;
                    console.log("message loaded" + data);
                });            
            });

and of course I am trying to display the message with {{message}} . 当然,我尝试使用{{message}}显示消息。

Sadly this method is not working, I am trying a second way which involves using ng-show . 遗憾的是,此方法无法正常工作,我正在尝试使用ng-show的第二种方法。

Can I pass a NodeJS function to a ng-show ? 我可以将NodeJS函数传递给ng-show吗? Something similar to: 类似于:

<div class="alert alert-danger" ng-show="isAuthenticated = true">
                 <p> Failed login message </p>
</div>

If not, how can I display a failed login message to a static html file without using jade, handlebars or any other template engine? 如果没有,如何在不使用jade,车把或任何其他模板引擎的情况下向静态html文件显示失败的登录消息?

A couple things here that may help: 这里有几件事情可能会有所帮助:

Firstly, you should avoid sending multiple responses for a request, so you should remove res.redirect('/login'); 首先,您应避免发送多个请求响应,因此应删除res.redirect('/login'); .

Your res.json({"message": "Failed login. Wrong Username or Password"}); 您的res.json({"message": "Failed login. Wrong Username or Password"}); ends the response so the redirect never runs and redirecting would defeat the purpose of you displaying a error message for the user. 终止响应,因此重定向永远不会运行,并且重定向将使您无法为用户显示错误消息。 Further, you should also send the proper status code to the client (401 Unauthorized in this case) to let it know that the request was not successful. 此外,您还应该将正确的状态代码发送给客户端(在这种情况下为401 未经授权 ),以使其知道请求未成功。 For example: 例如:

function isLoggedIn(req, res, next) {
    console.log('here is Authenticated', req.isAuthenticated());
    if (req.isAuthenticated()){
       return next();
    } else{
       res.status(401).json({"message": "Failed login. Wrong Username or Password"});
    }
}

On the client side, $http returns a response object with a .data property that contains the payload of the response you sent from the server. 在客户端, $http返回带有.data属性的响应对象,该属性包含您从服务器发送的响应的有效负载。 You need to use that to access the JSON that has the message for the failed login. 您需要使用它来访问包含失败登录消息的JSON。 Also note that you're sending an object to the client, so you'll need to access the actual text of the string via res.data.message . 还要注意,您正在将对象发送给客户端,因此您需要通过res.data.message访问字符串的实际文本。

Also note that with $http , only a response with a status code in the 200 range is considered successful. 还要注意,使用$http只有状态码在200范围内的响应才被认为是成功的。 So if the login fails, you can handle the failed login in an error handler. 因此,如果登录失败,则可以在错误处理程序中处理失败的登录。 This way your UI can decide what to do based on whether the login was successful or not. 这样,您的UI可以根据登录是否成功来决定要做什么。 For example: 例如:

 var app = angular.module('myApp', []);
 app.controller('myCtrl', function($scope, $http) {

     $http.get("/login").then(function(res){
        // Do stuff if login is successful
     })
     .catch(function(res) {
       $scope.message = res.data.message;
     })

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

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