为什么我的控制器会破坏我的代码?

时间:2016-08-07 02:10:51

标签: javascript json cordova ionic-framework

我正在运行一个离子项目,并使用Ionic Lab工具实时显示它。这是非常好的,因为每次我修改一个HTML文档,它会显示正在发生的事情。

现在,我在controllers.js的底部插入了一个控制器,它只是打破了我的项目。我的意思是,如果我从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中调用该控制器:

.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 .

当然是启动我的控制器的线路......

2 个答案:

答案 0 :(得分:3)

两个问题:

控制器;上有一个尾随分号(LoginCtrl)。

你的AcctCtrl无法流畅地连接到你的角度模块,因为它上方的控制器上有一个尾随的分号。

第二,

这一行有太多关闭)

JSON.stringify(firebase.auth().currentUser));

应该是:

JSON.stringify(firebase.auth().currentUser);

答案 1 :(得分:2)

从行:41删除simi-colon,这会将引用链打破为angular。