Angularjs Ionic-无法在函数内使用$IonicModel
我正在尝试在异步操作后使用$ionicModal。我所做的是将以下代码放入回调函数中,该函数在关联服务执行$http调用后执行:Angularjs Ionic-无法在函数内使用$IonicModel,angularjs,ionic-framework,Angularjs,Ionic Framework,我正在尝试在异步操作后使用$ionicModal。我所做的是将以下代码放入回调函数中,该函数在关联服务执行$http调用后执行: $ionicModal.fromTemplateUrl('templates/modals/profile-update.html', { scope: $scope }) .then(instance => { vm.modal = instance; }) 上面的代码放在控制器的主块中时工作正常。但是,在本例中,我希望它位于函数openProfileP
$ionicModal.fromTemplateUrl('templates/modals/profile-update.html', {
scope: $scope
})
.then(instance => {
vm.modal = instance;
})
上面的代码放在控制器的主块中时工作正常。但是,在本例中,我希望它位于函数openProfilePopup中。我尝试了以下代码,但它不起作用,模式没有出现,控制台中也没有显示错误:
activate();
function activate() {
profPopupService
.get()
.then(openProfilePopup, handleError);
}
function openProfilePopup(profile) {
vm.profile = profile;
$ionicModal.fromTemplateUrl('templates/modals/profile-update.html', {
scope: $scope
})
.then(instance => {
vm.modal = instance;
})
.catch(error => {
$log.log(error);
});
vm.modal.show();
}
下面是模态的模板代码。我为模态使用了一个单独的控制器,而不是与上面代码关联的控制器
<ion-modal-view cache-view="false">
<ion-header-bar class="bar bar-header bar-dark">
<div class="buttons">
<button class="button-icon light ion-ios-arrow-back" ng-click="dashboard.modal.hide()"></button>
</div>
<h1 id="page-title" class="title">Update Your Profile</h1>
</ion-header-bar>
<ion-content class="profile-popup" ng-controller="ProfilePopupCtrl as pp">
<form name="profileForm" method="POST" ng-submit="pp.update(profileForm.$valid)" novalidate>
<!-- Not displaying here -->
</form>
</ion-content>
</ion-modal-view>
调用内部的show方法。然后
或
在函数外部定义模态。当函数调用完成时,只需调用modal的show方法
.controller('MyController', function($scope, $ionicModal) {
$ionicModal.fromTemplateUrl('my-modal.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
现在在函数中
function openProfilePopup(profile) {
$scope.profile = profile;
$scope.openModal();
}
如果您想使用像vm.something这样的controller作为语法,您可以使用controller。内部调用show方法。然后
或
在函数外部定义模态。当函数调用完成时,只需调用modal的show方法
.controller('MyController', function($scope, $ionicModal) {
$ionicModal.fromTemplateUrl('my-modal.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
现在在函数中
function openProfilePopup(profile) {
$scope.profile = profile;
$scope.openModal();
}
如果您想像vm一样使用controller,您可以将其用作语法。我把Show放在openProfilePopup中,它是.then.You调用的openProfilePopup中的一个命名函数,但我的意思是调用.inside,然后调用$ionicModal.fromTemplateUrl,如回答中所示。对不起,没有注意到。然后在您的第一个代码中。问题在于,在profPopupService完成$http服务之前,模态的控制器ProfilePopupCtrl正在初始化。这时我想打开模态,用从服务中获得的数据初始化它的控制器。如果我将show放在$ionicModal的回调中,弹出窗口将打开,但控制器ProfilePopupCtrl将没有数据可使用,因为profPopupService尚未通过openProfilePopup提供数据。这是最初所做的。我把Show放在openProfilePopup中,它是.then.You调用的openProfilePopup中的一个命名函数,但我的意思是调用.inside,然后调用$ionicModal.fromTemplateUrl,如回答中所示。对不起,没有注意到。然后在您的第一个代码中。问题在于,在profPopupService完成$http服务之前,模态的控制器ProfilePopupCtrl正在初始化。这时我想打开模态,用从服务中获得的数据初始化它的控制器。如果我将show放在$ionicModal的回调中,则弹出窗口将打开,但控制器ProfilePopupCtrl将没有数据可使用,因为profPopupService尚未通过openProfilePopup提供数据。