我正在运行一个 ionic 项目,并使用 Ionic Lab 工具对其进行实时可视化。这非常好,因为每次我修改 html 文档时,它都会实时显示发生的情况。
现在,我在controllers.js 的底部插入了一个 Controller ,它只是破坏了我的项目。我的意思是,如果我从controllers.js 文件中取出代码,该项目会正常显示,但如果我放入它,它只会使整个项目变成空白。就像上面什么都没有一样。
这是我添加的代码:
.controller('AccountCtrl', function($scope, $state, $cordovaOauth) {
$scope.$on('$ionicView.enter', function() {
//Authentication details.
JSON.stringify(firebase.auth().currentUser));
//Get logged in user credentials.
var user = firebase.auth().currentUser;
var name, email, photoUrl, provider;
if (user != null) {
name = user.displayName;
email = user.email;
photoUrl = user.photoURL;
provider = user.provider;
}
//Set Profile Image.
profileImage = photoUrl;
//Set Profile Name.
profileName = name;
//It is now up to you to set provider and email!
})
});
这是我的完整文档:
angular.module('starter.controllers', [])
.controller('LoginCtrl', function($scope, $state, $cordovaOauth) {
$scope.signIn = function() {
$state.go('tab.dash');
};
$scope.facebook = function() {
var facebookAppId = "198040820571843";
$cordovaOauth.facebook(facebookAppId, ["public_profile", "email"]).then(function(response) {
var credential = firebase.auth.FacebookAuthProvider.credential(response.access_token);
loginWithCredential(credential, 'Facebook');
}, function(error) {
//User cancelled login. Hide the loading modal.
});
}
$scope.twitter = function() {
var twitterKey = "aJWByCgPhUgYZJMojyFeH2h8F";
var twitterSecret = "XxqKHi6Bq3MHWESBLm0an5ndLxPYQ2uzLtIDy6f9vgKKc9kemI";
$cordovaOauth.twitter(twitterKey, twitterSecret).then(function(response) {
var credential = firebase.auth.TwitterAuthProvider.credential(response.oauth_token,
response.oauth_token_secret);
loginWithCredential(credential, 'Twitter');
}, function(error) {
//User cancelled login. Hide the loading modal.
});
};
loginWithCredential = function(credential, provider) {
firebase.auth().signInWithCredential(credential)
.then(function(response) {
//User logged in through provider.
$state.go('tab.dash');
})
.catch(function(error) {
//Show error message.
var errorCode = error.code;
});
};
});
.controller('AccountCtrl', function($scope, $state, $cordovaOauth) {
$scope.$on('$ionicView.enter', function() {
//Authentication details.
JSON.stringify(firebase.auth().currentUser));
//Get logged in user credentials.
var user = firebase.auth().currentUser;
var name, email, photoUrl, provider;
if (user != null) {
name = user.displayName;
email = user.email;
photoUrl = user.photoURL;
provider = user.provider;
}
//Set Profile Image.
profileImage = photoUrl;
//Set Profile Name.
profileName = name;
//It is now up to you to set provider and email!
})
});
我真的不明白发生了什么,它只会破坏一切!
我在 app.js 中调用该 Controller :
.state('tab.account', {
url: '/account',
views: {
'tab-account': {
templateUrl: 'templates/tab-account.html'
}
},
controller: 'AccountCtrl'
})
如果有人有任何帮助...谢谢!
编辑,在控制台中,这是错误:
?ionicplatform=android:29 Uncaught SyntaxError: Unexpected token . http://localhost:8100/js/controllers.js Line: 43console.(anonymous function) @ ?ionicplatform=android:29
controllers.js:43 Uncaught SyntaxError: Unexpected token .
这当然是启动我的 Controller 的行......
最佳答案
两个问题:
Controller LoginCtrl
上有一个尾随分号 (;
)。
由于上面的 Controller 上有尾随分号,您的 AcctCtrl
无法流畅地附加到您的 Angular 模块。
第二,
这一行有太多结束 )
JSON.stringify(firebase.auth().currentUser));
应该是:
JSON.stringify(firebase.auth().currentUser);
关于javascript - 为什么我的 Controller 会破坏我的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38810276/