AngularJs $ http变量未定义

时间:2015-01-28 00:52:19

标签: javascript angularjs http

我有ng-controller

groupifyApp.controller('DashboardCtrl', function ($rootScope, $scope, $routeParams, $route, $http) {
$scope.page = {title:"Preferences", dashboardActive: "active", manageActive: "", inboxActive:"", preferencesActive:""};
$scope.user = {};
//$scope.user = {"name":"Test User","cal":{"entries":[{"id":0,"day":0,"start":1.25,"duration":1.0},{"id":1,"day":0,"start":4.0,"duration":1.75},{"id":2,"day":0,"start":4.75,"duration":0.5},{"id":3,"day":0,"start":5.75,"duration":0.75},{"id":4,"day":0,"start":6.5,"duration":0.25},{"id":5,"day":0,"start":7.5,"duration":0.25},{"id":6,"day":0,"start":9.5,"duration":1.75},{"id":7,"day":0,"start":10.75,"duration":0.5},{"id":8,"day":0,"start":11.75,"duration":0.75},{"id":9,"day":1,"start":0.75,"duration":0.5},{"id":10,"day":1,"start":1.25,"duration":0.25},{"id":11,"day":1,"start":2.0,"duration":0.5},{"id":12,"day":1,"start":2.75,"duration":0.5},{"id":13,"day":1,"start":3.5,"duration":0.25},{"id":14,"day":1,"start":4.5,"duration":0.5},{"id":15,"day":1,"start":6.5,"duration":1.5},{"id":16,"day":1,"start":7.75,"duration":0.75},{"id":17,"day":1,"start":8.25,"duration":0.25},{"id":18,"day":1,"start":11.25,"duration":0.5},{"id":19,"day":2,"start":0.0,"duration":0.25},{"id":20,"day":2,"start":1.25,"duration":0.75},{"id":21,"day":2,"start":1.75,"duration":0.25},{"id":22,"day":2,"start":2.5,"duration":0.5},{"id":23,"day":2,"start":4.25,"duration":1.25},{"id":24,"day":2,"start":5.75,"duration":0.75},{"id":25,"day":2,"start":6.75,"duration":0.75},{"id":26,"day":2,"start":7.75,"duration":0.75},{"id":27,"day":2,"start":9.0,"duration":1.0},{"id":28,"day":2,"start":9.75,"duration":0.25},{"id":29,"day":2,"start":10.75,"duration":0.5},{"id":30,"day":3,"start":0.25,"duration":0.5},{"id":31,"day":3,"start":0.75,"duration":0.25},{"id":32,"day":3,"start":1.25,"duration":0.25},{"id":33,"day":3,"start":2.5,"duration":0.5},{"id":34,"day":3,"start":3.5,"duration":0.75},{"id":35,"day":3,"start":4.0,"duration":0.25},{"id":36,"day":3,"start":4.75,"duration":0.5},{"id":37,"day":3,"start":5.5,"duration":0.5},{"id":38,"day":3,"start":6.0,"duration":0.25},{"id":39,"day":3,"start":6.5,"duration":0.25},{"id":40,"day":3,"start":7.25,"duration":0.25},{"id":41,"day":3,"start":8.0,"duration":0.5},{"id":42,"day":3,"start":8.5,"duration":0.25},{"id":43,"day":3,"start":9.5,"duration":0.75},{"id":44,"day":3,"start":10.75,"duration":0.5},{"id":45,"day":3,"start":11.25,"duration":0.25},{"id":46,"day":3,"start":11.75,"duration":0.25},{"id":47,"day":4,"start":1.5,"duration":1.5},{"id":48,"day":4,"start":2.0,"duration":0.25},{"id":49,"day":4,"start":3.25,"duration":0.5},{"id":50,"day":4,"start":4.0,"duration":0.25},{"id":51,"day":4,"start":4.75,"duration":0.25},{"id":52,"day":4,"start":7.25,"duration":2.0},{"id":53,"day":4,"start":7.75,"duration":0.25},{"id":54,"day":4,"start":8.25,"duration":0.25},{"id":55,"day":4,"start":9.75,"duration":0.75},{"id":56,"day":4,"start":11.0,"duration":0.25}]},"age":-1900,"username":"testUser","id":0};
$scope.user = function() { return getFullUser($http)};

alert(JSON.stringify($scope.user));

});

一个功能

function getFullUser($http) {
return
    $http({method: "GET", url: "api/user", headers: {
        'Content-Type': 'application/json', 'token': localStorage.token}})
          .success(function(data) {
              return data;
          });

}

我在这里发现了一个类似的问题enter link description here 但无法将其解决方案映射到我的问题

当我的 getFullUser 函数重新 $ scope.user 时,它会未定义。 当我使用静态对象(注释掉的行)时,它可以正常工作。

但response.data与静态版本相同。 我将其结构与 JSON.stringify(response.data)

进行了比较

你知道为什么$ scope.user仍未定义吗?

功能和控制器位于 2个不同的文件中,我不想将它们合并在一起。

所以duplicate answers没有帮助。

由于

2 个答案:

答案 0 :(得分:1)

你有几个问题。

  1. 我认为您的代码不是您发布的内容。无论您遇到什么其他问题,发布的代码都会定义变量$http
  2. 您将$scope.user设置为不是用户(如注释掉的代码所示),而不是承诺提取用户(这是明智的),但是将返回获取用户的承诺的函数。
  3. 重新检查你的代码,找出你错放了一些花括号的位置。

答案 1 :(得分:0)

因此,在阅读了有关AJAX调用和异步编程的几个小时后,我发现了它。 我把它放在这里给任何可能会被卡住的人。 再次感谢@PSL让我走上正轨。

过了一会儿,试图解决我的问题,我最终得到了这个:

groupifyApp.controller('DashboardCtrl', function ($rootScope, $scope, $routeParams, $route, $http) {
$scope.page = {title:"Preferences", dashboardActive: "active", manageActive: "", inboxActive:"", preferencesActive:""};
$scope.user = {};
//$scope.user = {"name":"Test User","cal":{"entries":[{"id":0,"day":0,"start":1.25,"duration":1.0},{"id":1,"day":0,"start":4.0,"duration":1.75},{"id":2,"day":0,"start":4.75,"duration":0.5},{"id":3,"day":0,"start":5.75,"duration":0.75},{"id":4,"day":0,"start":6.5,"duration":0.25},{"id":5,"day":0,"start":7.5,"duration":0.25},{"id":6,"day":0,"start":9.5,"duration":1.75},{"id":7,"day":0,"start":10.75,"duration":0.5},{"id":8,"day":0,"start":11.75,"duration":0.75},{"id":9,"day":1,"start":0.75,"duration":0.5},{"id":10,"day":1,"start":1.25,"duration":0.25},{"id":11,"day":1,"start":2.0,"duration":0.5},{"id":12,"day":1,"start":2.75,"duration":0.5},{"id":13,"day":1,"start":3.5,"duration":0.25},{"id":14,"day":1,"start":4.5,"duration":0.5},{"id":15,"day":1,"start":6.5,"duration":1.5},{"id":16,"day":1,"start":7.75,"duration":0.75},{"id":17,"day":1,"start":8.25,"duration":0.25},{"id":18,"day":1,"start":11.25,"duration":0.5},{"id":19,"day":2,"start":0.0,"duration":0.25},{"id":20,"day":2,"start":1.25,"duration":0.75},{"id":21,"day":2,"start":1.75,"duration":0.25},{"id":22,"day":2,"start":2.5,"duration":0.5},{"id":23,"day":2,"start":4.25,"duration":1.25},{"id":24,"day":2,"start":5.75,"duration":0.75},{"id":25,"day":2,"start":6.75,"duration":0.75},{"id":26,"day":2,"start":7.75,"duration":0.75},{"id":27,"day":2,"start":9.0,"duration":1.0},{"id":28,"day":2,"start":9.75,"duration":0.25},{"id":29,"day":2,"start":10.75,"duration":0.5},{"id":30,"day":3,"start":0.25,"duration":0.5},{"id":31,"day":3,"start":0.75,"duration":0.25},{"id":32,"day":3,"start":1.25,"duration":0.25},{"id":33,"day":3,"start":2.5,"duration":0.5},{"id":34,"day":3,"start":3.5,"duration":0.75},{"id":35,"day":3,"start":4.0,"duration":0.25},{"id":36,"day":3,"start":4.75,"duration":0.5},{"id":37,"day":3,"start":5.5,"duration":0.5},{"id":38,"day":3,"start":6.0,"duration":0.25},{"id":39,"day":3,"start":6.5,"duration":0.25},{"id":40,"day":3,"start":7.25,"duration":0.25},{"id":41,"day":3,"start":8.0,"duration":0.5},{"id":42,"day":3,"start":8.5,"duration":0.25},{"id":43,"day":3,"start":9.5,"duration":0.75},{"id":44,"day":3,"start":10.75,"duration":0.5},{"id":45,"day":3,"start":11.25,"duration":0.25},{"id":46,"day":3,"start":11.75,"duration":0.25},{"id":47,"day":4,"start":1.5,"duration":1.5},{"id":48,"day":4,"start":2.0,"duration":0.25},{"id":49,"day":4,"start":3.25,"duration":0.5},{"id":50,"day":4,"start":4.0,"duration":0.25},{"id":51,"day":4,"start":4.75,"duration":0.25},{"id":52,"day":4,"start":7.25,"duration":2.0},{"id":53,"day":4,"start":7.75,"duration":0.25},{"id":54,"day":4,"start":8.25,"duration":0.25},{"id":55,"day":4,"start":9.75,"duration":0.75},{"id":56,"day":4,"start":11.0,"duration":0.25}]},"age":-1900,"username":"testUser","id":0};
getFullUser($http, $scope.user);
//$scope.slots = $scope.user.slots;

alert(JSON.stringify($scope.user));

});

我的功能

function getFullUser($http, user) {
    $http({method: "GET", url: "api/user", headers: {
        'Content-Type': 'application/json', 'token': localStorage.token}})
          .then(function(result) {
              user = result.data;
              alert(JSON.stringify(user));
          });

}

请注意,它们都在两个不同的文件中,因此只需拨打$scope.user = result.data; 一个选项。

我还不确定这是如何工作的,因为在其他编程语言中,例如C#,Java等,我的getFullUser函数只会在自己的范围内更改其变量 user 外面什么都不会发生。

然后在阅读了有关回调和其他解决方案之后,我提出了不移交变量(例如 user )的想法,而是交出函数 setUserCallback 将在从我的RestService获取结果时执行。

所以我最终得到了这个(效果很好):

groupifyApp.controller('DashboardCtrl', function ($rootScope, $scope, $routeParams, $route, $http) {
    $scope.page = {title:"Preferences", dashboardActive: "active", manageActive: "", inboxActive:"", preferencesActive:""};
    $scope.user = {};
    //$scope.user = {"name":"Test User","cal":{"entries":[{"id":0,"day":0,"start":1.25,"duration":1.0},{"id":1,"day":0,"start":4.0,"duration":1.75},{"id":2,"day":0,"start":4.75,"duration":0.5},{"id":3,"day":0,"start":5.75,"duration":0.75},{"id":4,"day":0,"start":6.5,"duration":0.25},{"id":5,"day":0,"start":7.5,"duration":0.25},{"id":6,"day":0,"start":9.5,"duration":1.75},{"id":7,"day":0,"start":10.75,"duration":0.5},{"id":8,"day":0,"start":11.75,"duration":0.75},{"id":9,"day":1,"start":0.75,"duration":0.5},{"id":10,"day":1,"start":1.25,"duration":0.25},{"id":11,"day":1,"start":2.0,"duration":0.5},{"id":12,"day":1,"start":2.75,"duration":0.5},{"id":13,"day":1,"start":3.5,"duration":0.25},{"id":14,"day":1,"start":4.5,"duration":0.5},{"id":15,"day":1,"start":6.5,"duration":1.5},{"id":16,"day":1,"start":7.75,"duration":0.75},{"id":17,"day":1,"start":8.25,"duration":0.25},{"id":18,"day":1,"start":11.25,"duration":0.5},{"id":19,"day":2,"start":0.0,"duration":0.25},{"id":20,"day":2,"start":1.25,"duration":0.75},{"id":21,"day":2,"start":1.75,"duration":0.25},{"id":22,"day":2,"start":2.5,"duration":0.5},{"id":23,"day":2,"start":4.25,"duration":1.25},{"id":24,"day":2,"start":5.75,"duration":0.75},{"id":25,"day":2,"start":6.75,"duration":0.75},{"id":26,"day":2,"start":7.75,"duration":0.75},{"id":27,"day":2,"start":9.0,"duration":1.0},{"id":28,"day":2,"start":9.75,"duration":0.25},{"id":29,"day":2,"start":10.75,"duration":0.5},{"id":30,"day":3,"start":0.25,"duration":0.5},{"id":31,"day":3,"start":0.75,"duration":0.25},{"id":32,"day":3,"start":1.25,"duration":0.25},{"id":33,"day":3,"start":2.5,"duration":0.5},{"id":34,"day":3,"start":3.5,"duration":0.75},{"id":35,"day":3,"start":4.0,"duration":0.25},{"id":36,"day":3,"start":4.75,"duration":0.5},{"id":37,"day":3,"start":5.5,"duration":0.5},{"id":38,"day":3,"start":6.0,"duration":0.25},{"id":39,"day":3,"start":6.5,"duration":0.25},{"id":40,"day":3,"start":7.25,"duration":0.25},{"id":41,"day":3,"start":8.0,"duration":0.5},{"id":42,"day":3,"start":8.5,"duration":0.25},{"id":43,"day":3,"start":9.5,"duration":0.75},{"id":44,"day":3,"start":10.75,"duration":0.5},{"id":45,"day":3,"start":11.25,"duration":0.25},{"id":46,"day":3,"start":11.75,"duration":0.25},{"id":47,"day":4,"start":1.5,"duration":1.5},{"id":48,"day":4,"start":2.0,"duration":0.25},{"id":49,"day":4,"start":3.25,"duration":0.5},{"id":50,"day":4,"start":4.0,"duration":0.25},{"id":51,"day":4,"start":4.75,"duration":0.25},{"id":52,"day":4,"start":7.25,"duration":2.0},{"id":53,"day":4,"start":7.75,"duration":0.25},{"id":54,"day":4,"start":8.25,"duration":0.25},{"id":55,"day":4,"start":9.75,"duration":0.75},{"id":56,"day":4,"start":11.0,"duration":0.25}]},"age":-1900,"username":"testUser","id":0};
    getFullUser($http, function(data) {
        $scope.user = data;
    });
    //$scope.slots = $scope.user.slots;

    alert(JSON.stringify($scope.user));

});

我的功能:

function getFullUser($http, setUserCallback) {
    $http({method: "GET", url: "api/user", headers: {
        'Content-Type': 'application/json', 'token': localStorage.token}})
          .then(function(result) {
              setUserCallback(result.data);
          });

}

我不熟悉这种回调思维模式。事先定义方法应该做什么是/是一个新概念。