AngularJS:AngularUI路由器中状态间共享控制器
我的ionic应用程序中有这些router.jsAngularJS:AngularUI路由器中状态间共享控制器,angularjs,angular-ui-router,ionic-framework,Angularjs,Angular Ui Router,Ionic Framework,我的ionic应用程序中有这些router.js myapp.config(function($stateProvider, $urlRouterProvider) { $stateProvider .state('app', { url: "/app", abstract: true, templateUrl: "templates/menu.html", controller: 'AppCtrl' }) .state('app.myp
myapp.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})
.state('app.myprofile', {
url: "/myprofile",
abstract: true,
views: {
'menuContent': {
controller : "myprofileCtrl",
templateUrl: "templates/myprofile.html"
}
}
})
.state('app.myprofile.info', {
url: "/info",
controller: "profileinfoCtrl",
templateUrl: "templates/profile/info.html"
})
.state('app.myprofile.location', {
url: "/location",
controller: "profilelocationCtrl",
templateUrl: "templates/profile/location.html"
})
$urlRouterProvider.otherwise('/');
});
我需要一种在myprofile状态和myprofile.location状态以及myprofile.info状态之间共享控制器作用域的方法
我使用的是离子框架
在myprofileCtrl中
myapp.controller("myprofileCtrl",function($scope,Authy ,Auth,$http ,$rootScope){
$scope.view_loading = true;
Authy.can_go().then(function(data){
$scope.profile =data.profile;
});
});
在profilelocationCtrl中
myapp.controller("profilelocationCtrl",function($scope,Authy ,Auth,$http ,$rootScope){
console.log($scope.profile);
});
我在控制台中未定义我认为问题在于,由于在ajax调用后设置了
$scope.profile
,当最初达到myprofile.location
状态时,在父$scope
上仍然未定义profile变量,您可以做的是$watch
配置文件变量,或者,当您从服务器调用接收事件时,最好使用$broadcast
向下广播事件
$broadcast
将事件向下分派到所有子作用域,因此您可以执行以下操作:
myapp.controller("myprofileCtrl",function($scope,Authy ,Auth,$http ,$rootScope){
$scope.view_loading = true;
Authy.can_go().then(function(data){
$scope.profile =data.profile;
$scope.$broadcast('profileSet', data.profile);
});
});
在myprofile.location`状态中:
myapp.controller("profilelocationCtrl",function($scope,Authy,Auth,$http,$rootScope){
$scope.$on('profileSet', function(event, data) {
console.log(data);
});
}
});
看看我刚刚制作的演示plunk。你已经是了,因为location是myprofile的子项,它将使用它的父控制器。我将进行更新,向你展示我的problem@RemonAmin你尝试过mu建议吗?当我重新加载视图时,它会工作,但当我单击一个选项卡并导航到位置选项卡时,应用程序会中断!!!你应该好好休息一下