Javascript 从$http请求获取数据的角度未定义

Javascript 从$http请求获取数据的角度未定义,javascript,angularjs,controller,get,response,Javascript,Angularjs,Controller,Get,Response,所以,我想从一个API(由Laravel提供)获取数据。我正在Angular中使用$http获取此数据,当我将其发送到视图时,它工作正常,但当我想在控制器中使用数据时,它不会: HTML: <!doctype html> <html ng-app="todoApp"> <head> <script src="http://code.jquery.com/jquery-1.10.2.js"></script>

所以,我想从一个API(由Laravel提供)获取数据。我正在Angular中使用$http获取此数据,当我将其发送到视图时,它工作正常,但当我想在控制器中使用数据时,它不会:

HTML:

<!doctype html>
<html ng-app="todoApp">
    <head>
        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
        <script src="/js/MainController.js"></script>
    </head>
    <body ng-controller="MainController as myControl">
        <h1>Todo-list:</h1>
        <div>
            <table>
                <tr>
                    <th>Note:</th>
                    <th>Author:</th>
                    <th>Project:</th>
                    <th>Created:</th>
                    <th>Deadline:</th>
                </tr>
                <tr ng-repeat="todo in myControl.todos">
                    <td> {{ todo.content }} </td>
                    <td> {{ todo.user }} </td>
                    <td> {{ todo.project }} </td>
                    <td> {{ todo.created }} </td>
                    <td> {{ todo.deadline }} </td>
                    <td><button ng-click="myControl.showUpd(todo.id)">Update</button></td>
                </tr>
            </table>
        </div>
    </body>
</html>
angular.module('todoApp', []).controller('MainController', function($scope, $http) {
    var thisApp = this;

    // Below works without any problems when accessing it from the html-view:

    $http({method : 'GET', url : 'http://localhost:8000/notes'})
        .then (function(response) {
            thisApp.todos = response.data;
        }, function() {
            alert("Error getting todo notes");
        });

    // But this function doen't work as intended

    thisApp.showUpd = function(noteID) {
        $http({method : 'GET', url : 'http://localhost:8000/notes/' + noteID})
            .then (function(response) {
                thisApp.getNote = response.data;
                console.log(thisApp.getNote); // MainController.js:20
                alert("Got data");
            }, function() {
                alert("Error getting todo note");
            });

        console.log(thisApp.getNote); // MainController.js:26
    }
});
控制台提供以下信息:

  • MainController.js:26未定义
  • MainController.js:20对象{id:1,内容:“asdasd”,完成时间:0,截止日期:“2016-01-14 10:29:38”}
我的问题是,当我在
$http
内部访问
thisApp.getNote
时,它可以正常工作,但当我尝试在外部访问它时,我只会得到未定义。我已经尝试将
thisApp
更改为
$scope
,并且我还尝试在函数中声明另一个变量,但没有任何差异


有人能发现我的问题吗?

第26行的console.log包含在您使用$http创建的承诺之外。这意味着它将在承诺解决之前运行,因此在属性设置之前运行,以便在该时间点返回未定义。您需要等到您解决了尝试使用该财产的承诺之后

您应该在中创建回调。然后为$http调用创建回调

thisApp.showUpd = function(noteID) {
    $http({method : 'GET', url : 'http://localhost:8000/notes/' + noteID})
        .then (function(response) {
            thisApp.getNote = response.data;
            console.log(thisApp.getNote); // MainController.js:20
            alert("Got data");
            thisApp.logMe(); // Call the callback here to run the code that depends on thisApp.getNote being defined.
        }, function() {
            alert("Error getting todo note");
        });
}

// Create a callback function that will be called when the $http promise has been resolved but not until then.
thisApp.logMe = function() { 
    console.log(thisApp.getNote); // MainController.js:26
}

$http是异步的,这意味着您在第17行附近发出请求,然后执行第26行。由于请求尚未返回,
thisApp.getNote
尚未设置,因此未定义。它在$http的“外部”可用,但只有在设置之后才可用,直到$http调用被解决后才可用,这将在将来某个时候发生(如果有的话)。@sethflowers我明白了,你有什么好的解决方案吗?比如说把它放在另一个函数里或者别的什么东西里?这其实没什么问题。它正在做它应该做的事情。您发出一个http请求,当它返回时,您将数据设置到某个地方。你已经在这样做了。在设置它之前,它将永远不会是除未定义之外的任何内容。