Javascript $scope.modal在$ionicPlatform.ready上未定义
我一直在到处寻找这个问题的答案;我不介意,但它实际上在早些时候起作用了。如果localStorage中没有存储任何用户访问令牌,我将尝试在加载应用程序时在Ionic应用程序中打开一个模式 这是我正在使用的代码: JS HTMLJavascript $scope.modal在$ionicPlatform.ready上未定义,javascript,angularjs,ionic-framework,ngcordova,Javascript,Angularjs,Ionic Framework,Ngcordova,我一直在到处寻找这个问题的答案;我不介意,但它实际上在早些时候起作用了。如果localStorage中没有存储任何用户访问令牌,我将尝试在加载应用程序时在Ionic应用程序中打开一个模式 这是我正在使用的代码: JS HTML 正如我之前所说,它实际上是工作的,在启动时按预期打开模式。然后它就停止了工作,并产生了一个我之前没有明显原因遇到的错误。错误如下: ypeError: Cannot read property 'show' of undefined at Scope.$scope.o
正如我之前所说,它实际上是工作的,在启动时按预期打开模式。然后它就停止了工作,并产生了一个我之前没有明显原因遇到的错误。错误如下:
ypeError: Cannot read property 'show' of undefined
at Scope.$scope.openModal (http://localhost:8100/js/controllers.js:24:17)
at http://localhost:8100/js/controllers.js:60:14
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:53329:19
at Object.ionic.Platform.ready (http://localhost:8100/lib/ionic/js/ionic.bundle.js:2135:9)
at Object.self.ready (http://localhost:8100/lib/ionic/js/ionic.bundle.js:53327:26)
at new <anonymous> (http://localhost:8100/js/controllers.js:54:18)
at invoke (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17762:17)
at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17770:27)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:22326:28
at self.appendViewElement (http://localhost:8100/lib/ionic/js/ionic.bundle.js:56883:24) <ion-nav-view name="tab-league" class="view-container tab-content" nav-view="active" nav-view-transition="ios">
ypeError:无法读取未定义的属性“show”
在作用域上。$Scope.openModal(http://localhost:8100/js/controllers.js:24:17)
在http://localhost:8100/js/controllers.js:60:14
在http://localhost:8100/lib/ionic/js/ionic.bundle.js:53329:19
在Object.ionic.Platform.ready(http://localhost:8100/lib/ionic/js/ionic.bundle.js:2135:9)
准备好了(http://localhost:8100/lib/ionic/js/ionic.bundle.js:53327:26)
在纽约(http://localhost:8100/js/controllers.js:54:18)
在调用时(http://localhost:8100/lib/ionic/js/ionic.bundle.js:17762:17)
在Object.instantiate(http://localhost:8100/lib/ionic/js/ionic.bundle.js:17770:27)
在http://localhost:8100/lib/ionic/js/ionic.bundle.js:22326:28
在self.appendViewElement(http://localhost:8100/lib/ionic/js/ionic.bundle.js:56883:24)
非常感谢您的帮助,我无法理解问题所在。尝试在
$ionicPlatform.ready()
的回调函数中创建模态
检查此代码:
$scope.createModal = function(){
if($scope.modal){
$scope.openModal();
}
else{
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function (modal) {
$scope.modal = modal;
$scope.modal.show();
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
}
}
调用上述函数以代替$ionicPlatform.ready()
中的$scope.openModal()
请尝试将要更新
$scope.$apply()
变量的代码包装在$scope.$apply()
中,如下所示:
$scope.$apply(function () {
<your codes that update $scope.<your variables>>
});
$scope.$apply(函数(){
});
这是因为$ionicPlatform.ready()
中的代码将在另一轮JavaScript中运行。AngularJS不知道$scope
变量已经更新,因此不会更新HTML元素
将代码包装在$scope.$apply()
中将调用$scope.$digest
将$scope
变量更改应用于HTML
$scope.createModal = function(){
if($scope.modal){
$scope.openModal();
}
else{
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function (modal) {
$scope.modal = modal;
$scope.modal.show();
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
}
}
$ionicPlatform.ready(function(){
...
...
$scope.createModal();
...
...
})
$scope.$apply(function () {
<your codes that update $scope.<your variables>>
});