我需要在第一个 Controller 的方法中从第二个 Controller 调用初始化用户设置值(设置为根范围)的方法。
成功设置根范围后返回前提并继续执行第一个 Controller 的第二个方法。
我尝试使用发射和广播示例,但没有运气。
有人可以给我建议如何正确地做到这一点吗?
我使用的是 Angular 1.2 和 Ionic 1,beta 13
非常感谢您的帮助。
模板:
<ion-view title="{{ 'DAYS_RESULTS' | translate }}" >
<ion-content ng-controller="DailyStatsCtrl" ng-init="setData()">
代码:
// First cotroller call setData in ng-init
angular.module('starter')
// Controller definition
.controller('DailyStatsCtrl', function($scope, $rootScope, $ionicSlideBoxDelegate, $timeout , $ionicLoading , $q, $translate, DialsComputeService, cordova, LocalStorService, $ionicPopup, $state, $ionicNavBarDelegate, testService, $ionicPlatform) {
$scope.setData = function() {
$ionicPlatform.ready(function() {
$timeout(function() {
$scope.$emit("onSomething", "");
alert("TEST");
}, 1000);
});
};
在第二个 Controller 中:
angular.module('starter')
// Controller definition
.controller('SettingsCtrl', function($scope, $rootScope, $ionicLoading, $ionicPlatform, LocalStorService, $timeout, $translate, $ionicPopup, $state, $ionicNavBarDelegate) {
$scope.getUserSettigns = function() {
alert("received");
$scope.test = 'event received';
}
$scope.$on('onSomething', function(e) {
$scope.getUserSettigns();
});
最佳答案
在 ionic 中,在 Controller View 加载之前无法调用 Controller 函数。如果您的第二个 Controller 的 View 已加载,您可以执行以下操作:
secondview.html:
设置 ion-content id 以获取 Controller 范围
<ion-view title="second">
<ion-content id="secondView">
</ion-content>
</ion-view>
第二个 Controller :
app.controller('SecondController', function($scope, $http) {
$scope.someFunction = function(args) {
alert(args.Message);
};
});
代码:
app.controller('CodeController', function($scope) {
$scope.$on('$ionicView.beforeEnter', function(){
angular.element($('#secondView')).scope().someFunction({ Message: "Hello!" });
});
});
希望这有帮助。
关于javascript - AngularJS + Ionic - 如何使用延迟 promise 从第一个 Controller 调用第二个 Controller 上的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27381593/