node.js ng-click 后 AngularJS 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19286270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:55:43  来源:igfitidea点击:

AngularJS redirection after ng-click

javascriptnode.jsangularjsredirectmongoose

提问by GuillaumeA

I have a REST API that read/save data from a MongoDB database. The application I use retrieves a form and create an object (a job) from it, then save it to the DB. After the form, I have a button which click event triggers the saving function of my controller, then redirects to another url.

我有一个 REST API,可以从 MongoDB 数据库读取/保存数据。我使用的应用程序检索表单并从中创建对象(作业),然后将其保存到数据库中。在表单之后,我有一个按钮,单击事件触发我的控制器的保存功能,然后重定向到另一个 url。

Once I click on the button, I am said that the job has well been added to the DB but the application is jammed and the redirection is never called. However, if I reload my application, I can see that the new "job" has well been added to the DB. What's wrong with this ??? Thanks !

单击该按钮后,我会说该作业已很好地添加到数据库中,但应用程序被卡住并且从未调用重定向。但是,如果我重新加载我的应用程序,我可以看到新的“作业”已很好地添加到数据库中。这有什么问题???谢谢 !

Here is my code:

这是我的代码:

Sample html(jade) code:

示例 html(jade) 代码:

button.btn.btn-large.btn-primary(type='submit', ng:click="save()") Create

Controller of the angular module:

角度模块的控制器:

function myJobOfferListCtrl($scope, $location, myJobs) {

    $scope.save = function() {
        var newJob = new myJobs($scope.job);
        newJob.$save(function(err) {
            if(err)
                console.log('Impossible to create new job');
            else {
                console.log('Ready to redirect');
                $location.path('/offers');
            }
        });     
    };    
}

Configuration of the angular module:

角度模块的配置:

var myApp = angular.module('appProfile', ['ngResource']);

myApp.factory('myJobs',['$resource', function($resource) {
    return $resource('/api/allMyPostedJobs',
            {},
            {
                save: {
                    method: 'POST'
                }   
            });
}]);

The routing in my nodejs application :

我的 nodejs 应用程序中的路由:

app.post('/job', pass.ensureAuthenticated, jobOffers_routes.create);

And finally the controller of my REST API:

最后是我的 REST API 的控制器:

exports.create = function(req, res) {
    var user = req.user;
    var job = new Job({ user: user, 
                        title: req.body.title,
                        description: req.body.description,
                        salary: req.body.salary,
                        dueDate: new Date(req.body.dueDate),
                        category: req.body.category});
    job.save(function(err) {
        if(err) {
            console.log(err);
            res.redirect('/home');
        } 
        else {
            console.log('New job for user: ' + user.username + " has been posted."); //<--- Message displayed in the log    
            //res.redirect('/offers'); //<---- triggered but never render
            res.send(JSON.stringify(job));
        }
    });
};

回答by GuillaumeA

I finally found the solution ! The issue was somewhere 18inches behind the screen....

我终于找到了解决方案!问题出在屏幕后面 18 英寸的某个地方....

I modified the angular application controller like this :

我像这样修改了角度应用程序控制器:

$scope.save = function() {
        var newJob = new myJobs($scope.job);
        newJob.$save(function(job) {
            if(!job) {
                $log.log('Impossible to create new job');
            }
            else {
                $window.location.href = '/offers';
            }
        });
    };    

The trick is that my REST api returned the created job as a json object, and I was dealing with it like it were an error ! So, each time I created a job object, I was returned a json object, and as it was non null, the log message was triggered and I was never redirected. Furthermore, I now use the $window.location.hrefproperty to fully reload the page.

诀窍是我的 REST api 将创建的作业作为 json 对象返回,而我正在处理它,就像它是一个错误一样!因此,每次我创建一个作业对象时,都会返回一个 json 对象,并且由于它非空,因此触发了日志消息并且我从未被重定向。此外,我现在使用该$window.location.href属性来完全重新加载页面。