访问范围 属性 inside 指令

Accessing scope property inside directive

我正在调用指令:

<latest-survey survey="latestSurvey"></latest-survey>

我的指令代码是:

function latestSurvey(){
    return {
        restrict: 'E',
        templateUrl: 'js/modules/survey/latest.html',
        scope: {
            survey: '=survey'
        },
        link: function(scope, element, attrs){
            console.log(scope);
            console.log(scope.survey);
        }
    }
}

console.log(scope)行输出范围:

Scope {$id: "007", $$childTail: null, $$childHead: null, $$prevSibling: null, $$nextSibling: null…}
$$asyncQueue: Array[0]
$$childHead: null
$$childTail: null
$$destroyed: false
$$isolateBindings: Object
$$listenerCount: Object
$$listeners: Object
$$nextSibling: 
$$childScopeClass
$$phase: null
$$postDigestQueue: Array[0]
$$prevSibling: null
$$watchers: Array[3]
$id: "007"
$parent: 
$$childScopeClass
$root: Scope
survey: Object
this: Scope
__proto__: Scope

如您所见,survey 列在那里,我可以在控制台中浏览它以查看其属性。但是,下一行 console.log(scope.survey) 输出:

undefined

如何在作用域内访问这个对象?

调查是否在控制器中异步加载(latestSurvey)? scope.surveyundefined 的原因之一可能是因为请求未完成且项目为空。

我建议您尝试设置 $scope.latestSurvey 一些虚拟数据,看看问题是否仍然存在。