我是AngularJS / Ionic的新手,我遇到了以下代码的问题:
.controller('myCtrl', function ($scope, $http, $ionicPopover, $state, $cordovaOauth){
//The following is used in the ng-click of a button
$scope.doAction = function () {
$cordovaOauth.google("xxxxxx.apps.googleusercontent.com", ["email"]).then(function (result) {
// results
alert(JSON.stringify(result));
$scope.google_access_token = result.access_token;
var http = $http({
url: 'https://www.googleapis.com/oauth2/v3/userinfo',
method: 'GET',
params: {
access_token: result.access_token
}
});
http.then(function (data) {
var user_data = data.data;
//The code is reaching here, as the alert is successfully shown with data
alert("Name: " + user_data.name + "\nEmail: " + user_data.email
+"\nPicture:" + user_data.picture);
userInfo.fullName = user_data.name;
userInfo.userId = user_data.email;
userInfo.img = user_data.picture;
localStorage.setItem('loggedInUser', user_data.email);
//But this is not taking me to the other controller
$state.go('tabsController.someOtherController');
});
}, function (error) {
// error
alert(error);
});
}
}
正如我在代码中的注释中所提到的,执行到达http.then内部,但它没有使用$ state.go调用其他控制器。如果我把它放在doAction()之前,那么它就会被调用,这意味着控制器本身没有任何问题。有人可以请帮助确定我在这里做错了什么吗?
答案 0 :(得分:0)
控制器是 NOT 状态。它们在run
文件的app.js
子句中定义。
要使$state.go
生效,您需要指定run
中定义的州名称。
示例:
对于此声明
.run(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('home', { ... })
.state('main', { abstract: true, ... })
.state('main.first', {
controller: 'firstCtrl'
})
.state('main.second', {
controller: 'secondCtrl'
});
});
这些是有效的:
$state.go('home');
$state.go('main.first');
$state.go('main.second');