2017-01-03 12 views
1

我試圖在之後使用$ ionicModal 進行異步操作。我所做的是把下面的代碼執行相關的業務執行$ HTTP調用後的回調函數內:當它置於控制器的主要區塊內

$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()函數啓動模態數據和數據傳遞到模式的控制器:

function ProfilePopupCtrl($log, $scope) { 
    const vm = this; 
    vm.user = $scope.$parent.dashboard.profile; 
    ....... 
} 

回答

0

調用show方法中.then()

function openProfilePopup(profile) { 
    vm.profile = profile; 

    $ionicModal.fromTemplateUrl('templates/modals/profile-update.html', { 
     scope: $scope 
    }) 
    .then(instance => { 
     vm.modal = instance; 
     vm.modal.show(); 
    }) 
    .catch(error => { 
     $log.log(error); 
    }); 
} 

定義函數外部的模態。在函數調用完成後,請調用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

+0

這就是最初做。我將Show()放在openProfilePopup()中,作爲.then()回調的命名函數。 –

+0

你在'openProfilePopup'裏面調用了,但是我的意思是在'$ ionicModal.fromTemplateUrl()'的'.then()'裏面調用,如回答 –

+0

所示。很抱歉,沒有注意到你的第一個代碼中的.then()。問題在於模型的控制器(ProfilePopupCtrl)在profPopupService完成$ http服務之前被初始化。那就是當我想打開模式並用從服務中獲得的數據初始化它的控制器時。如果我將show()放在$ ionicModal的回調函數()中,彈出窗口將打開,但控制器(ProfilePopupCtrl)將沒有數據可用,因爲profPopupService尚未通過openProfilePopup提供數據。 –