Angularjs 角度$scope数据不会更新

Angularjs 角度$scope数据不会更新,angularjs,node.js,api,express,scope,Angularjs,Node.js,Api,Express,Scope,我正在编写一个连接到第三方api的应用程序 api使用一个auth令牌系统,因此我有一个async node js函数,它首先请求令牌,然后使用该令牌检索一些数据 问题是,当数据更改时,angualr$scope不会更新,因此即使在节点js api调用中抛出错误,页面也会显示相同的数据 **get-saleLead-data.js** // app/get-SalesLead-data.js // modules ===========================

我正在编写一个连接到第三方api的应用程序

api使用一个auth令牌系统,因此我有一个async node js函数,它首先请求令牌,然后使用该令牌检索一些数据

问题是,当数据更改时,angualr$scope不会更新,因此即使在节点js api调用中抛出错误,页面也会显示相同的数据

    **get-saleLead-data.js** 

    // app/get-SalesLead-data.js
    // modules =================================================

    var http = require('http');
    var express = require('express')
    var async = require('async');

    module.exports = function(app) {


       Using async to preform async api calls and passing data between them

        async.waterfall([

            // First function is requesting the auth token

            requestToken = function(callback) {

                var options = {
                    "host": "********",
                    "path": '************'
                    "method": "PUT",
                    "headers": {
                        "Content-Type": "application/json",
                    }
                };

                var login = JSON.stringify({

                    "username": "********",

                    "password": "********",

                    "client_ip_address": "********"

                });

                var req = http.request(options, function(res) {
                    res.on('data', function(body) {
                        var body = JSON.parse(body);
                        var token = body.token;
                        console.log(token);
                        callback(null, token);
                    });
                });

                req.on('error', function(e) {
                    console.log('problem with request: ' + e.message);
                });

                req.write(login);
                req.end();
            },

            // Second function is using the auth token receieved from the first function to get sales lead dat
            getData = function(arg1, callback) {

                // Geting the sales id from the url and using it in the api request.
                app.get('/', function(req, res) {

                    app.set('salesLeadId', req.query.id);
                    var token = arg1;
                    var auth = 'Basic ' + new Buffer('********' + ':' + token).toString('base64');
                    var path = '****************' + app.get('salesLeadId');
                    var options = {
                        "host": "********",
                        "path": path,
                        "method": "GET",
                        "headers": {
                            "Content-Type": "application/json",
                            "Authorization": auth
                        }
                    };

                    var data = '';

                    // Assinging response data to to data
                    var req = http.request(options, function(res) {
                        res.on('data', function(chunk) {
                            data += chunk;
                        });

                        // Creating sales lead api so the front end can make requests
                        res.on('end', function(res) {
                            var body = JSON.parse(data);
                            console.log(body);
                            app.get('/api/salesLead', function(req, res) {
                                return res.json(body);
                                $scope.$apply();
                            });

                        })

                    });

                    req.on('error', function() {
                        alert('error');
                    });


                    res.sendFile('index.html', {
                        root: '../vpbx/public/views'
                    });

                    req.end();

                });

            }

        ], function(err, result) {

        });
    };
稍微运行一下代码

get-salesLead-data.js有一个异步瀑布函数,它首先使用http PUT调用第三方rest api并返回一个身份验证令牌。然后将该令牌传递到Async water的第二个函数中,该函数用于发出http GET请求以获取销售线索数据

下面是节点异步api调用

    **get-saleLead-data.js** 

    // app/get-SalesLead-data.js
    // modules =================================================

    var http = require('http');
    var express = require('express')
    var async = require('async');

    module.exports = function(app) {


       Using async to preform async api calls and passing data between them

        async.waterfall([

            // First function is requesting the auth token

            requestToken = function(callback) {

                var options = {
                    "host": "********",
                    "path": '************'
                    "method": "PUT",
                    "headers": {
                        "Content-Type": "application/json",
                    }
                };

                var login = JSON.stringify({

                    "username": "********",

                    "password": "********",

                    "client_ip_address": "********"

                });

                var req = http.request(options, function(res) {
                    res.on('data', function(body) {
                        var body = JSON.parse(body);
                        var token = body.token;
                        console.log(token);
                        callback(null, token);
                    });
                });

                req.on('error', function(e) {
                    console.log('problem with request: ' + e.message);
                });

                req.write(login);
                req.end();
            },

            // Second function is using the auth token receieved from the first function to get sales lead dat
            getData = function(arg1, callback) {

                // Geting the sales id from the url and using it in the api request.
                app.get('/', function(req, res) {

                    app.set('salesLeadId', req.query.id);
                    var token = arg1;
                    var auth = 'Basic ' + new Buffer('********' + ':' + token).toString('base64');
                    var path = '****************' + app.get('salesLeadId');
                    var options = {
                        "host": "********",
                        "path": path,
                        "method": "GET",
                        "headers": {
                            "Content-Type": "application/json",
                            "Authorization": auth
                        }
                    };

                    var data = '';

                    // Assinging response data to to data
                    var req = http.request(options, function(res) {
                        res.on('data', function(chunk) {
                            data += chunk;
                        });

                        // Creating sales lead api so the front end can make requests
                        res.on('end', function(res) {
                            var body = JSON.parse(data);
                            console.log(body);
                            app.get('/api/salesLead', function(req, res) {
                                return res.json(body);
                                $scope.$apply();
                            });

                        })

                    });

                    req.on('error', function() {
                        alert('error');
                    });


                    res.sendFile('index.html', {
                        root: '../vpbx/public/views'
                    });

                    req.end();

                });

            }

        ], function(err, result) {

        });
    };
下面是访问/api/salesLead的服务 创建使用http GET请求调用后端的函数。然后返回销售线索数据

**salesLeadService.js**

    angular.module('SalesLeadService', []).service('SalesLeadService', function($http, $location, $rootScope) {

        var urlBase = '/api/salesLead'

        this.getSalesLead = function (data) {
            return $http.get(urlBase, data)
        };
    });
下面是报价控制器。这将调用上面的服务并将数据分配给
$scope.salesLead

**offer.js**

    // Form controller
    // =============================================================================
    angular.module('offerController', []).controller('offerController', function($scope, $http, SalesLeadService, $timeout) {

        // GETTING THE DATE-TIME STAMP
        $scope.getDate = new Date();
        $scope.date = Date();

        // CALLING THE FUNCTION FROM THE SALES LEAD SERVICE

        SalesLeadService.getSalesLead()
            .then(function(response) {
                    $scope.salesLead = response.data;
                    $scope.$applyAsync();
            });
    });
请注意,我曾尝试使用
$scope.$apply
,但运气不佳。 谢谢你的帮助 谢谢

试着用这种方式处理对象

HTML

使用
{{object.salesLead}}

我想这对你有用

offer.js

angular.module('offerController', []).controller('offerController', function($scope, $http, SalesLeadService, $timeout) {

    // GETTING THE DATE-TIME STAMP
    $scope.getDate = new Date();
    $scope.date = Date();

    // CALLING THE FUNCTION FROM THE SALES LEAD SERVICE

     $scope.object = {salesLead:''};
    SalesLeadService.getSalesLead()
        .then(function(response) {
                $scope.object.salesLead = response.data;
                $scope.$applyAsync();
        });
});

我发现问题不在于前端,而在于后端

问题是我有一个嵌套的路由


为了解决这个问题,我完全重写了我的路线。

感谢您的回复。我累了,它仍然没有更新。没有抛出错误。在做了一些toruble拍摄之后,我发现没有更新的不是角度,而是节点异步API调用。我测试了localhost:8081/api/salesLead,得到了正确的JSON响应。在node js第三方api调用更新后,我再次运行了它,它仍然将相同的JSON主体发送到前端。