我想访问我的角度控制器的范围以外的控制器。我正在使用外部js库执行某些操作,并在完成时执行某些回调。在其中一个回调angular.element(document.findElementById('elementId').scope()
返回undefined
。 但是在第一个 angular.element(document.findElementById('elementId').scope()
返回有效范围之后执行的第二个回调中。angular.element(..)。scope()首先返回undefined
下面是一些示例代码
<body id="authcontroller" ng-controller="AuthenticationController as auth">
<script>
window.externalLibObj = {
onCallback1: function(){
// Undefined value for scope
var scope = angular.element(document.getElementById('authcontroller')).scope();
},
onCallback2(): function(){
// Valid value for scope
var scope = angular.element(document.getElementById('authcontroller')).scope();
}
};
</script>
</body>
其中这些回调的执行顺序是onCallback1
然后onCallback2
。 var scope
未定义在onCallback1
但为什么在onCallback1
未定义scope
在onCallback2
的值?
感谢您的帮助
你也可以说是调用这些回调的代码? –
@PeterLaBanca调用这些回调的代码在js lib加载后自动执行,我从不明确调用调用这些回调的代码 –
这两个回调是否立即执行?这听起来像是一个时间问题,在第二次回调被调用之前,范围还没有准备好。 –