【问题标题】:Angular $scope data wont updateAngular $scope 数据不会更新
【发布时间】:2017-06-23 06:31:43
【问题描述】:

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

api 使用身份验证令牌系统,因此我有一个异步节点 js 函数,它首先请求令牌,然后使用该令牌检索一些数据。

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

稍微运行一下代码。

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

这是 Node Async 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,但运气不佳。 任何帮助表示赞赏 谢谢

【问题讨论】:

    标签: angularjs node.js api express scope


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      尝试这样的对象

      $scope.object = {salesLead : ''};
      
      $scope.object.salesLead = response.data;
      

      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 拍摄后,我发现它不是 angular 没有更新它实际上是节点异步 API 调用。我测试了 localhost:8081/api/salesLead 并得到了正确的 JSON 响应。然后我在节点 js 3rd 方 api 调用更新后再次运行它,它仍在向前端发送相同的 JSON 正文。
      猜你喜欢
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2019-06-05
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多