访问$ scope的属性时出现问题

时间:2019-03-11 22:08:57

标签: javascript angularjs

我不明白我的申请流程是什么。 在我的index.html中,对象的属性恢复得很好,但是在我的控制器中,它给了我不确定的结果。

Controller.js

angular
    .module('app.core')
    .controller('Controller', Controller);
function Controller($scope, Service) {
    $scope.configs = [];
    var getConfigs = function() {
        Service.getAll.go({
        }).$promise.then(function(successResponse) {
            $scope.configs = successResponse;
            console.log($scope.configs.name); // => Undefined
        }, function(err) {
            $scope.isLoading = false;
            //Function error
        });
    };
    getConfigs();
    console.log($scope.configs.name); // => Undefined
}

Index.html

<!DOCTYPE html>
<head>
    <title>test</title>
</head>
<body ng-app="app.core">
    <div ng-controller="Controller">
        <table >
            <tr>
                <th >Name</th>
                <th>Last Name Urgencia</th>
            </tr>
            <tr ng-repeat="cfg in configs">
                <td >{{cfg.name}}</td>
                <td >{{configuracion.lastName}}</td>
            </tr>
        </table>
    </div>
</body>
</html>

在index.html中,我可以看到属性名称和姓氏的值。 console.log,打印未定义

1 个答案:

答案 0 :(得分:0)

“ cfg in configs”循环遍历一个数组,但是您正在登录$ scope.configs.name,看到configs是一个数组,它将没有名为name的属性。将$ scope.configs [0] .name记录在回调函数中,它将记录数组第一个元素的name属性。

第二个console.log在getConfigs之后直接调用,因此回调尚未运行,并且$ scope.configs仍是初始化时的空数组。

相关问题